Zde*_*pič 1 uinavigationbar uiview ios
我在视图控制器中手动添加UINavigationBar loadView.
我正在使用制图(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
}
Run Code Online (Sandbox Code Playgroud)
代表方法:
public func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
return .TopAttached
}
Run Code Online (Sandbox Code Playgroud)
然而结果是小条,而不是状态栏下的扩展版.
如果要以编程方式添加导航栏,则需要注意,edgesForExtendedLayout任何条形定位API都不会相对于界面的布局指南.由于您现在已经指示要管理此栏,因此系统无法强制它是否应位于状态栏下.您需要做的是配置约束,使条形图始终相对于顶部布局指南定位.
所以对于初学者来说,让我们前往你的loadView方法:
let bar = UINavigationBar()
bar.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(bar)
self.navBar = bar
let topLayoutGuide = self.topLayoutGuide
Run Code Online (Sandbox Code Playgroud)
我们现在需要确保导航栏的底部相对于布局指南定位.所以我们所做的就是说导航栏的底部= layoutGuides的顶部+ 44.其中44是导航栏的合适高度.请记住,布局指南可以在您打电话时更改,因此始终使用布局指南并且永远不要对状态栏高度进行硬编码非常重要.
constrain(navBar) { bar in
bar.top == bar.superview!.top
bar.width == bar.superview!.width
bar.bottom == topLayoutGuide.top + 44
}
Run Code Online (Sandbox Code Playgroud)
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]))
Run Code Online (Sandbox Code Playgroud)
瞧!您现在有几行响应状态栏的自定义导航栏
| 归档时间: |
|
| 查看次数: |
875 次 |
| 最近记录: |