iOS 11 SearchBar in NavigationBar

https://stackoverflow.com/questions/45350035/ios-11-searchbar-in-navigationbar


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 setting it as the titleView of the navigationItem using navigationItem.titleView = searchBar.

However, in iOS 11 it does not seem to work anymore as expected. Have a look at the screens where we compare the same setup using iOS 10 and iOS 11

iOS 10 enter image description here

iOS 11 enter image description here

You can clearly see that the SearchBar increases the size of the NavigationBar but the bar buttons do not get aligned correctly. Also the searchBar does not use the available space on the left anymore.

Putting the searchBar into a wrapper view to get the cancel button on iPad as described here Cancel button is not shown in UISearchBar also does not seem work anymore since the searchBar is then not visible at all.

If anyone has similar issues or already knowns how to fix/improve this I would be very thankful.

This was built using Xcode 9 Beta 4. Maybe future releases will fix this issue.

UPDATE:

Since this does not get "fixed" we now decided to use following solution: We added a new UIBarButtonItem in the NavBar and when the user presses this button we then present a new ViewController where we only put a searchBar into the NavBar which looks good now. Using the selected answer may be the best solution.

share improve this question
 

4 Answers

up vote 20 down vote accepted

There's a new searchController property on navigationItem in iOS 11.

https://developer.apple.com/documentation/uikit/uinavigationitem/2897305-searchcontroller

Use like this...

if #available(iOS 11.0, *) {
     navigationItem.searchController = searchController
} else {
     // Fallback on earlier versions
     navigationItem.titleView = searchController?.searchBar
}
share improve this answer
 
1  
My answer is similar to @cook answer, but I thought the link and the code might help clarify a bit. –  Justin Domnitz  Aug 22 at 20:35
6  
This will not give the results as original described. Using the searchController property will put the search bar below the navigation. –  Molanda  Sep 26 at 1:08
 
This works on iPhone but doesn't work on iPad for me. –  Starchand  Oct 4 at 9:59

You can change the height of UISearchBar in iOS 11 by adding a constraint of height 44:

if #available(iOS 11.0, *) {
    searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
share improve this answer
 
 
This makes top and bottom bezel shorter. I tried this objective-c solution: [[self.notesSearchBar.heightAnchor constraintEqualToConstant:44.0] setActive:YES], but I dont like this solution because it makes top bottom margin thinner and weird. –  coolcool1994  Sep 30 at 12:59
 
This worked for me. It repositions my search bar maintaining the correct layout. –  Christian Ascone  Oct 4 at 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中,可以通过以下方式自定义NavigationBar: 1. 设置NavigationBar的背景色和透明度 ```swift // 设置NavigationBar的背景色 navigationController?.navigationBar.barTintColor = UIColor.red // 设置NavigationBar的透明度 navigationController?.navigationBar.isTranslucent = true ``` 2. 设置NavigationBar的标题和字体样式 ```swift // 设置NavigationBar的标题 navigationItem.title = "Custom Title" // 设置NavigationBar的字体样式 navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 18), NSAttributedString.Key.foregroundColor: UIColor.white] ``` 3. 设置NavigationBar的返回按钮和图片 ```swift // 设置NavigationBar的返回按钮 let backButton = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil) navigationItem.backBarButtonItem = backButton // 设置NavigationBar的返回图片 let backImage = UIImage(named: "back") navigationController?.navigationBar.backIndicatorImage = backImage navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage ``` 4. 隐藏NavigationBar ```swift // 隐藏NavigationBar navigationController?.navigationBar.isHidden = true ``` 5. 自定义NavigationBar的左右按钮 ```swift // 自定义NavigationBar的左按钮 let leftButton = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(leftButtonTapped)) navigationItem.leftBarButtonItem = leftButton // 自定义NavigationBar的右按钮 let rightButton = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(rightButtonTapped)) navigationItem.rightBarButtonItem = rightButton // 左按钮点击事件 @objc func leftButtonTapped() { // do something } // 右按钮点击事件 @objc func rightButtonTapped() { // do something } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值