以编程方式添加一个UINavigationBar,它确实覆盖了状态栏 [英] Programmatically add a UINavigationBar that does underlap the status bar

查看:66
本文介绍了以编程方式添加一个UINavigationBar,它确实覆盖了状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图控制器中手动添加UINavigationBar loadView

I am adding a UINavigationBar manually in a View Controllers loadView.

我正在使用制图( https://github.com/robb/Cartography )使用自动布局布局视图。

I am using Cartography (https://github.com/robb/Cartography) to layout views with Auto Layout.

self.edgesForExtendedLayout = UIRectEdge.None
        let navBar = UINavigationBar()
        navBar.delegate = self
        view.addSubview(navBar)

        constrain(navBar) { bar in
            bar.top == bar.superview!.top
            bar.centerX == bar.superview!.centerX
            bar.width == bar.superview!.width
        }

委托方法:

public func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return .TopAttached
    }

但结果是小条,不是状态栏下的扩展版本。

Yet the result is small bar, not the extended version under the status bar.

推荐答案

如果要以编程方式添加导航栏,则需要注意 edgesForExtendedLayout 并且任何条形定位API都不会相对于界面的布局指南。由于您现在已经表明要管理此栏,因此系统无法强制它是否应位于状态栏下。你需要做的是配置你的约束,使得栏总是相对于顶部布局指南定位。

If you are adding a navigation bar programmatically you need to be aware that edgesForExtendedLayout and any bar positioning API will not be relative to the layout guides of your interface. Since you have now indicated you want to manage this bar, the system can't enforce whether it should be under the status bar or not. What you need to do is configure your constraints such that the bar will always be positioned relative to the top layout guide.

所以对于初学者来说,你可以前往你的 loadView 方法:

So for starters lets head to your loadView method:

let bar = UINavigationBar()
bar.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(bar)
self.navBar = bar
let topLayoutGuide = self.topLayoutGuide

我们现在需要确保导航栏的底部相对于布局指南定位。所以我们所做的就是说导航栏的底部= layoutGuides的顶部+ 44.其中44是导航栏的合适高度。请记住,布局指南可以在您打电话时更改,因此始终使用布局指南并且永远不要对状态栏高度进行硬编码非常重要。

We now need to make sure that the navigation bar's bottom is positioned relative to the layout guide. So all we do is say that the bottom of the navigation bar = layoutGuides's top + 44. Where 44 is a decent height for a navigation bar. Remember the layout guide can change when you are in a phone call so its important to always use the layout guide and never hardcode the status bar height.

constrain(navBar) { bar in
    bar.top == bar.superview!.top
    bar.width == bar.superview!.width
    bar.bottom == topLayoutGuide.top + 44
}



使用NSLayoutConstraints的方法



Approach Using NSLayoutConstraints

let navBarTopConstraint = NSLayoutConstraint(item: bar, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bar]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["bar":bar])
let navBarBottomConstraint = NSLayoutConstraint(item: bar, attribute: .Bottom, relatedBy: .Equal, toItem: topLayoutGuide, attribute: .Top, multiplier: 1, constant: 44)
self.view.addConstraints([navBarTopConstraint,navBarBottomConstraint,horizontalConstraints]))

瞧!您现在有几行响应状态栏的自定义导航栏

Voila! You now have a custom navigation bar in a couple of lines that responds to the status bar

这篇关于以编程方式添加一个UINavigationBar,它确实覆盖了状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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