ios 如何更改导航栏的高度 - Swift 3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40751366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change height of Navigation Bar - Swift 3
提问by KirillC
I'm pretty sure this is not duplicate because I looked at other answers and they all outdated and do not provide exact answer.
我很确定这不是重复的,因为我查看了其他答案,它们都过时了,并且没有提供确切的答案。
I have Navigation Controller and several view controllers. I want to make Navigation Bar a bit taller so it would fit text size that I need. How can I do that ?
我有导航控制器和几个视图控制器。我想让导航栏高一点,以便它适合我需要的文本大小。我怎样才能做到这一点 ?
I tried this:
我试过这个:
UINavigationBar.appearance().frame = CGRect(x: 0.0, y: 0.0, width: 320.0, height: 210.0)
but nothing happens...
但什么也没发生……
Also, I wasn't able to add any constraints to the Nav Bar using Xcode layout buttons.
此外,我无法使用 Xcode 布局按钮向导航栏添加任何约束。
Hope somebody can help me to fix this issue. Thanks in advance!
希望有人能帮我解决这个问题。提前致谢!
回答by Frankie
Here is one way to do it:
这是一种方法:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
回答by kbokdia
You can write this code in class that extends UINavigationController
您可以在扩展 UINavigationController 的类中编写此代码
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let height = CGFloat(72)
navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
}
回答by Patricio Bravo
You can create a class based on UINavigationBar
like this
您可以基于UINavigationBar
这样创建一个类
class TTNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 55)
}
}
And add it to your NavigationController.
并将其添加到您的 NavigationController。
回答by HMHero
This might be better if your navigation bars in your app all have same adjusted height.
如果您的应用中的导航栏都具有相同的调整高度,这可能会更好。
extension UINavigationBar {
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 51)
}
}
回答by Alessio Campanelli
you can draw your navigation bar from xib (with your height) and then:
您可以从xib(使用您的身高)绘制导航栏,然后:
self.navigationController?.navigationBar.addSubview(yourNavBarView)
in alternative, if you want create UIView from code,
或者,如果您想从代码创建 UIView,
let viewNavBar = UIView(frame: CGRect(
origin: CGPoint(x: 0, y:0),
size: CGSize(width: self.view.frame.size.width, height: 100)))
and then adding to nav bar.
然后添加到导航栏。