简体   繁体   English

增加NavigationBar高度

[英]Increase NavigationBar height

I have the following code: 我有以下代码:

func navbarbutton() {
    UIView.animateWithDuration(0.2, animations: { () -> Void in
        let current = self.navigationController?.navigationBar.frame
        self.navigationController?.navigationBar.frame = CGRectMake(self.frame!.origin.x, self.frame!.origin.y, self.frame!.size.width, current!.size.height + 50)
        self.navigationController?.navigationBar.layoutIfNeeded()
    })
}

I'm able to increase the height of the navigation bar by 50 dp. 我能够将导航栏的高度增加50 dp。 That's not the issue for me. 这对我来说不是问题。 The issue I'm having is that the UIBarButtonItems are all aligned to the bottom. 我遇到的问题是UIBarButtonItems都与底部对齐。 How can I get them aligned to the top so that I can add more to the bottom myself? 如何让它们与顶部对齐,以便我可以自己添加更多底部? I'm getting something as per the image: 我按照图像得到了一些东西:

在此输入图像描述

Is it possible to get it aligned to the top? 有可能让它与顶部对齐吗?

Unfortunately you can't do that. 不幸的是你做不到。

The view hierarchy within the UINavigationBar is private so you can't manipulate it without iterating over the subviews and going all hacky with it. UINavigationBar的视图层次结构是私有的,因此您无法在不迭代子视图的情况下对其进行操作。 This probably isn't a good idea. 这可能不是一个好主意。

Out of curiosity I looked at the Messages app in iOS 10 using the view debugger because they obviously do this. 出于好奇,我使用视图调试器查看了iOS 10中的Messages应用程序,因为它们显然是这样做的。 They actually achieve the layout by adding their own UIButton to replace the back and rightBarButtonItem. 他们实际上通过添加自己的UIButton来替换back和rightBarButtonItem来实现布局。 This is something you would be able to do however they also set the alpha of one of the internal (and private) content views to zero so that the original content is no longer visible. 这是您可以执行的操作,但是他们还将其中一个内部(和私有)内容视图的alpha设置为零,以便原始内容不再可见。
This is something that you won't be able to do so easily unfortunately. 不幸的是,这是你无法轻易做到的事情。 You could try to hack things about until it works but remember you also need to handle pushes/pops, in call status bars, interactive transitions and rotation events. 你可以尝试破解它直到它工作,但记住你还需要处理推/弹,在调用状态栏,交互式转换和旋转事件。

If however you wasn't going for the iMessage style and just wanted to add some content underneath your navigation bar, why not look at pinning a UIVisualEffectView to the topLayoutGuide in your UIViewController ? 然而,如果你不打算对iMessage的风格,只是想增加你的导航栏下面一些内容,为什么不看钉扎UIVisualEffectViewtopLayoutGuide在你UIViewController You can get a fairly nice look pretty easily and it saves hacking stuff about a lot. 你可以很容易地获得一个相当不错的外观,它可以节省很多黑客的东西。 Here's an example: 这是一个例子:

例

Try this code: 试试这段代码:

Note: Code tested in Swift 3. 注意:代码在Swift 3中测试过。

Answer 1: Updated Answer 答案1: 更新了答案

class ViewController: UIViewController {

var customBar: UINavigationBar = UINavigationBar()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //Title
    title = "Some Title"

    // Add bar button item
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action:  #selector(addTapped))
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))

    self.customBar.frame = CGRect(x:0, y:0, width:view.frame.width, height:(navigationController?.navigationBar.frame.height)! + 50)  
    self.customBar.backgroundColor = UIColor.green
    self.view.addSubview(customBar)
}

func addTapped() {

    print("Button Pressed")

}

Output: 输出:

在此输入图像描述


Answer 2: 答案2:

override var isViewLoaded: Bool {

    // Add bar button item
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action:  #selector(addTapped))
    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(addTapped))

    //Vertical and Horizonal barButtonItem position offset
    navigationItem.leftBarButtonItem?.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 20), for: UIBarMetrics.default)

    navigationItem.rightBarButtonItem?.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: 20), for: UIBarMetrics.default)

    return true
}

func addTapped() {

    print("Button Pressed")

}

Note: Above code only works in isViewLoaded: Bool method.But, No luck.When, I tried this code in other viewLoad method. 注意:上面的代码只适用于isViewLoaded:Bool方法。但是,没有运气。当我在其他viewLoad方法中尝试此代码时。

Output 1: barButtonItem moved 20 pixel up vertically. 输出1: barButtonItem垂直向上移动20像素。

在此输入图像描述

Output 2: barButtonItem moved 20 pixel down vertically. 输出2: barButtonItem垂直向下移动20个像素。

在此输入图像描述

Hope, Above code fix your problem. 希望,上面的代码修复你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM