iOS 10 `rightBarButtonItem` 移出导航栏

iOS 10 `rightBarButtonItem` moves outside of 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;

我还在 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);

在 iOS11 中,排序按钮位于导航栏内搜索控制器的旁边,如下所示:

但是,在 iOS10 中,按钮完全移到了导航栏之外:

我的第一个想法是将按钮约束在willLayoutSubviews中,但是由于没有按钮的view组件,我无法将translatesAutoresizingMaskIntoConstraints设置为NO ]. 然后我通过完全删除搜索控制器来检查是否是将搜索控制器放置在导航栏内的问题。仍然(在 iOS 10 中)按钮停留在导航栏之外:

如何将 rightBarButtonItem 固定到 iOS 10 及更早版本 的导航栏内?

我需要释放我创建的原始按钮,以便它作为栏按钮项正确保留在导航栏中。

[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!