以编程方式添加一个不重叠状态栏的 UINavigationBar
Programmatically add a UINavigationBar that does underlap the status bar
我正在视图控制器中手动添加 UINavigationBar loadView
。
我正在使用 Cartography (https://github.com/robb/Cartography) 通过自动布局来布局视图。
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
}
结果是小条,不是状态条下的加长版
如果您以编程方式添加导航栏,您需要注意 edgesForExtendedLayout
和任何导航栏定位 API 都不会相对于您界面的布局指南。由于您现在已表示要管理此栏,因此系统无法强制它是否应位于状态栏下方。您需要做的是配置约束,使栏始终相对于顶部布局指南定位。
所以对于初学者来说,让我们转向您的 loadView
方法:
let bar = UINavigationBar()
bar.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(bar)
self.navBar = bar
let topLayoutGuide = self.topLayoutGuide
我们现在需要确保导航栏的底部相对于布局指南定位。所以我们所做的就是说导航栏的底部 = layoutGuides 的顶部 + 44。其中 44 是导航栏的合适高度。请记住,当您在 phone 调用中时,布局指南可能会发生变化,因此务必始终使用布局指南并且永远不要对状态栏高度进行硬编码。
使用制图的方法
constrain(navBar) { bar in
bar.top == bar.superview!.top
bar.width == bar.superview!.width
bar.bottom == topLayoutGuide.top + 44
}
使用 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]))
瞧!您现在有一个响应状态栏的几行自定义导航栏
我正在视图控制器中手动添加 UINavigationBar loadView
。
我正在使用 Cartography (https://github.com/robb/Cartography) 通过自动布局来布局视图。
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
}
结果是小条,不是状态条下的加长版
如果您以编程方式添加导航栏,您需要注意 edgesForExtendedLayout
和任何导航栏定位 API 都不会相对于您界面的布局指南。由于您现在已表示要管理此栏,因此系统无法强制它是否应位于状态栏下方。您需要做的是配置约束,使栏始终相对于顶部布局指南定位。
所以对于初学者来说,让我们转向您的 loadView
方法:
let bar = UINavigationBar()
bar.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(bar)
self.navBar = bar
let topLayoutGuide = self.topLayoutGuide
我们现在需要确保导航栏的底部相对于布局指南定位。所以我们所做的就是说导航栏的底部 = layoutGuides 的顶部 + 44。其中 44 是导航栏的合适高度。请记住,当您在 phone 调用中时,布局指南可能会发生变化,因此务必始终使用布局指南并且永远不要对状态栏高度进行硬编码。
使用制图的方法
constrain(navBar) { bar in
bar.top == bar.superview!.top
bar.width == bar.superview!.width
bar.bottom == topLayoutGuide.top + 44
}
使用 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]))
瞧!您现在有一个响应状态栏的几行自定义导航栏