iOS 11 SearchBar in NavigationBar

后端 未结 8 1847
陌清茗
陌清茗 2020-12-12 12:44

With iOS 11 Apple has redesigned the UISearchBar by making the corners rounder and the height bigger. Adding a UISearchBar to the navigationBar is pretty simple by just sett

相关标签:
8条回答
  • 2020-12-12 13:24

    I think you'll have to deal with setting the new UINavigationItem.searchController property to your UISearchController object. That's how you get the new effect as seen in Messages. It looks like the old behavior is just plain gone. I hope I'm wrong, but the whole API got an overhaul for 11. I know it's buggy in general so we'll see with newer betas and the GM if this gets fixed. (Writing at time of Beta 6)

    0 讨论(0)
  • 2020-12-12 13:26

    I was having the same issue and after a few day googling the issue, I found this page - https://translate.google.com/translate?hl=en&sl=zh-CN&u=http://www.jianshu.com/p/262f6e34a7d3&prev=search.

    This page leads to this git repo - https://github.com/DreamTravelingLight/searchBarDemo - this demo project shows how to use the old way with titleView to still have a searchBar without the sizing issue.

    The key lines are these

    _searchBar = [self addSearchBarWithFrame:CGRectMake(0, 0, kScreenWidth - 2 * 44 - 2 * 15, 44)];
    UIView *wrapView = [[UIView alloc] initWithFrame:_searchBar.frame];
    [wrapView addSubview:_searchBar];
    self.navigationItem.titleView = wrapView;
    

    If you embed the UISearchBar inside a view, and set that wrapView as the titleView, the UISearchBar will have the size you set for it, and will fit the nav bar as intended.

    Thanks, David

    0 讨论(0)
提交回复
热议问题