导航栏半透明(translucent)时announcement的约束写法
@(编程笔记)[iOS开发,UI布局]
本文使用SnapKit进行约束布局
当导航栏设置translucent
为false
时,导航栏不透明,UI布局从导航栏下开始。
但当导航栏的translucent
为true
,UI布局从顶端开始,这个时候如果announcement设置约束top.equalTo(superview)
,那么就会被导航栏盖住。
但是导航栏的translucent
是VC决定的,不能因为announcement就去改变,只能announcement去适应。
edgesForExtendedLayout
通过设置self.edgesForExtendedLayout = []
可以让VC布局从导航栏底部开始,但是缺陷很明显:
- VC的center会有一个偏移,那么你设定居中的东西都会变得不居中。
- (未经验证)因为VC布局是从导航栏底部开始,加上导航栏又有translucent,那么透出来的就会是上一个VC视图的底色
因此,这种方案没有采用。
self.topLayoutGuide.snp.bottom
// These objects may be used as layout items in the NSLayoutConstraint API
@available(iOS, introduced: 7.0, deprecated: 11.0, message: "Use view.safeAreaLayoutGuide.topAnchor instead of topLayoutGuide.bottomAnchor")
open var topLayoutGuide: UILayoutSupport { get }
使用self.topLayoutGuide.bottom
可以让announcement贴着导航栏底部。
announcement.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.right.equalTo(self.view)
make.height.equalTo(30)
}
网友评论