更改 width/height 嵌入导航控制器的 UINavigationBar?
Change width/height UINavigationBar embedded in a Navigation Controller?
可以将约束分配给手动添加到视图的 UINavigationBar。
但是当 UINavigationBar 被添加到视图并且视图嵌入到导航控制器中时,我无法为其添加约束。
我的objective是增加UINavigationBar
的高度
UINavigationBar 不允许为其自身分配约束。更改高度的唯一简单方法是在 提示符 属性 中添加 space。
要增加 UINavigationBar 的高度,有两个步骤:
- Sub-class UINavigationBar class 并覆盖为 UINavigationBar 提供高度的方法, 此 class 此后需要分配给 Navigation Controller
的 NavBar
注意:特定 Navigation Controller 下的所有视图都将具有新高度
代码片段:
class ModifiedNavBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
let screenWidth = UIScreen.mainScreen().bounds.width
let newSize:CGSize = CGSizeMake(screenWidth, 60)
return newSize
}
}
Note: The above step increases the height of the NavBar but it does not give you full customisation options. Adding a view gives you full control over the same.
- 以编程方式创建 view,然后将其添加到 UINavigationItem (titleView) Outlet:
代码片段:
class ViewController: UIViewController {
/*** UINavigationItem Outlet ***/
@IBOutlet weak var navbar: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
let view = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 90))
let label = UILabel(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 20))
let label2 = UILabel(frame: CGRectMake(0, 20, UIScreen.mainScreen().bounds.width, 20))
/*** First Label ***/
label.text = "Hello"
label.textAlignment = NSTextAlignment.Left
view.addSubview(label)
/***Second Label ***/
label2.text = "Hello2"
label2.textAlignment = NSTextAlignment.Left
view.addSubview(label2)
self.navbar.titleView = view
}
Note: If prompt is added to any of the UINavigationItem objects the size of the NavBar will increase
可以将约束分配给手动添加到视图的 UINavigationBar。
但是当 UINavigationBar 被添加到视图并且视图嵌入到导航控制器中时,我无法为其添加约束。
我的objective是增加UINavigationBar
的高度UINavigationBar 不允许为其自身分配约束。更改高度的唯一简单方法是在 提示符 属性 中添加 space。
要增加 UINavigationBar 的高度,有两个步骤:
- Sub-class UINavigationBar class 并覆盖为 UINavigationBar 提供高度的方法, 此 class 此后需要分配给 Navigation Controller 的 NavBar
注意:特定 Navigation Controller 下的所有视图都将具有新高度
代码片段:
class ModifiedNavBar: UINavigationBar {
override func sizeThatFits(size: CGSize) -> CGSize {
let screenWidth = UIScreen.mainScreen().bounds.width
let newSize:CGSize = CGSizeMake(screenWidth, 60)
return newSize
}
}
Note: The above step increases the height of the NavBar but it does not give you full customisation options. Adding a view gives you full control over the same.
- 以编程方式创建 view,然后将其添加到 UINavigationItem (titleView) Outlet:
代码片段:
class ViewController: UIViewController {
/*** UINavigationItem Outlet ***/
@IBOutlet weak var navbar: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
let view = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 90))
let label = UILabel(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 20))
let label2 = UILabel(frame: CGRectMake(0, 20, UIScreen.mainScreen().bounds.width, 20))
/*** First Label ***/
label.text = "Hello"
label.textAlignment = NSTextAlignment.Left
view.addSubview(label)
/***Second Label ***/
label2.text = "Hello2"
label2.textAlignment = NSTextAlignment.Left
view.addSubview(label2)
self.navbar.titleView = view
}
Note: If prompt is added to any of the UINavigationItem objects the size of the NavBar will increase