更改嵌入在导航控制器中的宽度/高度 UINavigationBar? [英] Change width/height UINavigationBar embedded in a Navigation Controller?

查看:26
本文介绍了更改嵌入在导航控制器中的宽度/高度 UINavigationBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以将约束分配给手动添加到视图中的 UINavigationBar.

但是当 UINavigationBar 被添加到视图中并且视图嵌入在导航控制器中时,我无法向其添加约束.

我的目标是增加 UINavigationBar

的高度

解决方案

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

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

  1. 继承UINavigationBar 类并覆盖为UINavigationBar 提供高度的方法,此后需要将此类分配给NavBar导航控制器
  2. 的强>

注意:该特定导航控制器下的所有视图都将具有新的高度

代码片段:

class ModifiedNavBar: UINavigationBar {覆盖 func sizeThatFits(size: CGSize) ->CG尺寸{让 screenWidth = UIScreen.mainScreen().bounds.width让 newSize:CGSize = CGSizeMake(screenWidth, 60)返回新大小}}

<块引用>

注意:上述步骤增加了 NavBar 的高度,但它没有为您提供完整的自定义选项.添加视图可让您完全控制它.

  1. 以编程方式创建视图,然后将其添加到UINavigationItem (titleView) Outlet:

代码片段:

class ViewController: UIViewController {/*** UINavigationItem Outlet ***/@IBOutlet 弱变量导航栏:UINavigationItem!覆盖 func viewDidLoad() {super.viewDidLoad()//在加载视图后做任何额外的设置,通常是从笔尖.super.viewDidLoad()让视图 = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 90))让标签 = UILabel(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 20))让 label2 = UILabel(frame: CGRectMake(0, 20, UIScreen.mainScreen().bounds.width, 20))/*** 第一个标签 ***/label.text = "你好"label.textAlignment = NSTextAlignment.Leftview.addSubview(标签)/***第二个标签***/label2.text = "Hello2"label2.textAlignment = NSTextAlignment.Leftview.addSubview(label2)self.navbar.titleView = 视图}

<块引用>

注意:如果将提示添加到任何UINavigationItem对象,NavBar的大小将增加

Constraints can be assigned to UINavigationBar that is added manually to the view.

But when the UINavigationBar is added to a view when and the view is embedded in a Navigation Controller, I am not able to add constraints to the same.

My objective is to increase the height of the UINavigationBar

解决方案

UINavigationBar does not allow to assign constraints to itself. The only easy way that the height can be changed is by adding a space to the prompt property.

To increase the the height of the UINavigationBar there are two steps:

  1. Sub-class the UINavigationBar class and overriding the method that give the height to the UINavigationBar, this class thereafter needs to be assigned to the NavBar of the Navigation Controller

Note: All the views under that particular Navigation Controller will have the new height

Code Snippet:

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. Create a view programmatically and then adding it to the UINavigationItem (titleView) Outlet:

Code Snippet:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆