Search code examples
iosobjective-cuinavigationbaruisearchcontroller

iOS 10 `rightBarButtonItem` moves outside of navigation bar


In my view controller, I display the search controller in the navigation bar.

/*set appearance of search controller */
    _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    _searchController.searchBar.delegate = self;
    _searchController.searchResultsUpdater = self;
    [_searchController.searchBar sizeToFit];
    _searchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    _searchController.hidesNavigationBarDuringPresentation = NO;
    _searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;

I also created a sort button with the help of the rightBarButtonItem.

    /*add drop down sort menu button to navigation bar */
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.titleView = self.searchController.searchBar;

    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                        style:UIBarButtonItemStyleDone
                                                                       target:self
                                                                       action:@selector(displaySortMenu:)];

    self.navigationItem.rightBarButtonItem = rightButtonItem;
    rightButtonItem.image = [UIImage imageNamed:@"Sort"];

    self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(6, 0, 0, 0);

In iOS 11, the sort button sits right next to the search controller inside the navigation bar, like so: enter image description here

However, in iOS 10, the buttons moves completely outside of the navigation bar: enter image description here

My first thought was to constrain the button in willLayoutSubviews, but as there is no view component of the button, I couldn't set translatesAutoresizingMaskIntoConstraints to NO. I then checked if it was an issue of placing the search controller inside the navigation bar by removing the search controller completely. Still (in iOS 10) the button stays outside of the navigation bar: enter image description here

How can I fix the rightBarButtonItem to inside the navigation bar for iOS 10 and earlier ?


Solution

  • I needed to release the original button I created for it to correctly stay in the navigation bar as a bar button item.

    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
        self.navigationController.navigationBar.translucent = NO;
        self.navigationItem.titleView = self.searchController.searchBar;
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                        style:UIBarButtonItemStyleDone
                                                                       target:self
                                                                       action:@selector(displaySortMenu:)];
    rightButtonItem.image = [UIImage imageNamed:@"Sort"];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release]; // <-- this is the new line!