gpt4 book ai didi

ios - 更改嵌入导航 Controller 的宽度/高度 UINavigationBar?

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:33 25 4
gpt4 key购买 nike

可以将约束分配给手动添加到 View 的 UINavigationBar

但是当 UINavigationBar 被添加到一个 View 并且该 View 被嵌入到一个导航 Controller 中时,我无法向它添加约束。

我的目标是增加 UINavigationBar

的高度

最佳答案

UINavigationBar 不允许为其自身分配约束。更改高度的唯一简单方法是在 prompt 属性中添加一个空格。

要增加 UINavigationBar 的高度,有两个步骤:

  1. 子类化 UINavigationBar 类并覆盖为 UINavigationBar 提供高度的方法,此后需要将此类分配给 NavBar 导航 Controller
  2. 的strong>

注意:该特定Navigation Controller 下的所有 View 都将具有新的高度

代码片段:

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.

  1. 以编程方式创建一个 View ,然后将其添加到UINavigationItem (titleView) 导出:

代码片段:

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

关于ios - 更改嵌入导航 Controller 的宽度/高度 UINavigationBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com