在我的视图控制器中,我在导航栏中显示搜索控制器。
/*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);
在iOS 11中,排序按钮位于导航栏内的搜索控制器旁边,如下所示:
但是,在iOS 10中,按钮完全移出导航栏:
我的第一个想法是将按钮限制在
willLayoutSubviews
,但因为没有
view
按钮的组件,我无法设置
translatesAutoresizingMaskIntoConstraints
到
NO
.
然后,我通过完全移除搜索控制器来检查是否存在将搜索控制器放置在导航栏中的问题。仍然(在iOS 10中)按钮位于导航栏之外:
如何修复
右巴布托主义
在iOS 10及更早版本的导航栏中
?