OGeek|极客世界-中国程序员成长平台

标题: iOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:12
标题: iOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar

我正在尝试使新的 iOS-11 searchBar 方法正常工作,但没有成功。 searchBar 出现了,但显示在左栏按钮下方,而不是旁边:

enter image description here

在横向模式下也是如此。 我使用的代码是:

self.searchResults = [[SearchResultsTable alloc] init];
self.searchResults.delegate = self;
searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResults];
searchController.searchResultsUpdater = self.searchResults;
searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                         target:self
                         actionselector(swapToListView];
if ([self.navigationItem respondsToSelectorselector(setSearchController]) {
    // only available from IOS-11 onwards
    self.navigationItem.title = nil;
    self.navigationItem.titleView = nil;
    [self.navigationItem setSearchController:searchController];

} else {
    // Fallback on earlier versions
    self.navigationItem.titleView = searchController.searchBar;
}

旧代码在 iOS-11 之前的系统上运行良好,但在 iOS-11 上存在其他问题,所以我更喜欢使用新方法。 有人知道我在这里做错了什么吗?



Best Answer-推荐答案


如果你想把搜索栏放在左侧导航项旁边,那么不管iOS版本如何,让你的搜索栏navigationItemtitleView,不要使用UINavigationItemsearchController 属性。

关于iOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47138413/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4