美文网首页
导航栏半透明(translucent)时announcement

导航栏半透明(translucent)时announcement

作者: tsiic | 来源:发表于2018-08-11 10:47 被阅读0次

    导航栏半透明(translucent)时announcement的约束写法

    @(编程笔记)[iOS开发,UI布局]
    本文使用SnapKit进行约束布局

    当导航栏设置translucentfalse时,导航栏不透明,UI布局从导航栏下开始。
    但当导航栏的translucenttrue,UI布局从顶端开始,这个时候如果announcement设置约束top.equalTo(superview),那么就会被导航栏盖住。

    但是导航栏的translucent是VC决定的,不能因为announcement就去改变,只能announcement去适应。

    edgesForExtendedLayout

    通过设置self.edgesForExtendedLayout = []可以让VC布局从导航栏底部开始,但是缺陷很明显:

    1. VC的center会有一个偏移,那么你设定居中的东西都会变得不居中。
    2. (未经验证)因为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)
            }
    

    相关文章

      网友评论

          本文标题:导航栏半透明(translucent)时announcement

          本文链接:https://www.haomeiwen.com/subject/saorbftx.html