rad*_*tiv 81 uinavigationbar ios swift ios11
我让我UISearchBar成为导航栏的一部分,如:
let searchBar = UISearchBar()
//some more configuration to the search bar
.....
navigationItem.titleView = searchBar
Run Code Online (Sandbox Code Playgroud)
更新到iOS 11我的应用程序中的搜索栏发生奇怪的事情.在iOS 10我之前我的导航栏看起来像:
现在iOS 11我有:
正如你所看到的那样,两个搜索栏的舍入存在差异,这并不困扰我.问题是搜索栏会增加导航栏的高度.所以,当我去另一个控制器时,它看起来很奇怪:
事实上,奇怪的黑线的高度加上当前导航栏的高度等于第二张图片中显示的导航栏的高度...
任何想法如何摆脱黑线和所有视图控制器具有一致的导航栏高度?
zgj*_*jie 65
您可以将高度为44的约束添加到iOS 11的搜索栏中.
if #available(iOS 11.0, *) {
searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
Run Code Online (Sandbox Code Playgroud)
// Objective-c
if (@available(iOS 11.0, *)) {
[searchBar.heightAnchor constraintEqualToConstant:44].active = YES;
}
Run Code Online (Sandbox Code Playgroud)
And*_*rew 57
在两种情况下,我在iOS5的NavigationBar下使用SearchBar获得黑线:
我的解决方案是:使用UISearchBar将此代码添加到我的ViewController:
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController.view setNeedsLayout]; // force update layout
[self.navigationController.view layoutIfNeeded]; // to fix height of the navigation bar
}
Run Code Online (Sandbox Code Playgroud)
Swift 4更新
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.view.setNeedsLayout() // force update layout
navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
}
Run Code Online (Sandbox Code Playgroud)
Mai*_*Mai 37
我相信在iOS 11中,UISearchBar现在的高度等于56,而UINavigationBar使用autolayout来适应其子视图,因此它增加了高度.如果您仍然希望UISearchBar与iOS 11之前的titleView一样,我发现最好的方法是在自定义视图中嵌入UISearchBar,并将此视图的高度设置为44,并将其分配给navigationItem.titleView
class SearchBarContainerView: UIView {
let searchBar: UISearchBar
init(customSearchBar: UISearchBar) {
searchBar = customSearchBar
super.init(frame: CGRect.zero)
addSubview(searchBar)
}
override convenience init(frame: CGRect) {
self.init(customSearchBar: UISearchBar())
self.frame = frame
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
searchBar.frame = bounds
}
}
class MyViewController: UIViewController {
func setupNavigationBar() {
let searchBar = UISearchBar()
let searchBarContainer = SearchBarContainerView(customSearchBar: searchBar)
searchBarContainer.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 44)
navigationItem.titleView = searchBarContainer
}
}
Run Code Online (Sandbox Code Playgroud)
小智 16
在viewDidLoad中的"ACKNOWLEDGMENTS"视图控制器上尝试此代码
self.extendedLayoutIncludesOpaqueBars = true
Run Code Online (Sandbox Code Playgroud)
谢谢你们!我终于找到了解决方案。
使用 UISearchBar 将以下代码添加到 ViewController。
viewDidLoad-(void)viewDidLoad
{
[super viewDidLoad];
self.extendedLayoutIncludesOpaqueBars = YES;
...
}
Run Code Online (Sandbox Code Playgroud)
override func viewDidLoad() {
super.viewDidLoad()
self.extendedLayoutIncludesOpaqueBars = true
}
Run Code Online (Sandbox Code Playgroud)
viewWillDisappear-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
// force update layout
[self.navigationController.view setNeedsLayout];
// to fix height of the navigation bar
[self.navigationController.view layoutIfNeeded];
}
Run Code Online (Sandbox Code Playgroud)
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.view.setNeedsLayout() // force update layout
navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30393 次 |
| 最近记录: |