【问题标题】:iOS 11 show searchBar in navigation bar next to leftBarButtonItemiOS 11 在 leftBarButtonItem 旁边的导航栏中显示 searchBar
【发布时间】:2018-04-18 16:51:54
【问题描述】:

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

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

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
                         action:@selector(swapToListView:)];
if ([self.navigationItem respondsToSelector:@selector(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 上存在其他问题,所以我更喜欢使用新方法。 有人知道我在这里做错了什么吗?

【问题讨论】:

    标签: ios uinavigationbar uibarbuttonitem ios11 uisearchcontroller


    【解决方案1】:

    如果你想将搜索栏放在左侧导航项旁边,那么无论 iOS 版本如何,都将搜索栏设为navigationItemtitleView,并且不要使用UINavigationItemsearchController 属性。

    【讨论】:

    • 是的,这是在 iOS-10 及更早版本中运行良好的旧方法。但在 iOS-11 中,这种方法存在重大的尺寸问题。我最终让它在 iOS-11 中工作,但需要手动调整大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    相关资源
    最近更新 更多