美文网首页
swift - 给一个视图添加约束

swift - 给一个视图添加约束

作者: GA_ | 来源:发表于2017-03-14 15:02 被阅读69次
注意:这个View是父视图  view1 和 view2 是互相约束的 
注意:尽量view2 为同一个层级 这样循环加view1的时候容易计算

view1.translatesAutoresizingMaskIntoConstraints = false
View.addSubview(view1)
View.addConstraint(NSLayoutConstraint(
                         item: <#AnyObject#>,  指定约束左边的视图view1
                         attribute: <#NSLayoutAttribute#>,  指定view1的属性attr1
                         relatedBy: <#NSLayoutRelation#>,  指定左右两边的视图的关系relation
                         toItem: <#AnyObject?#>,  指定约束右边的视图view2
                         attribute: <#NSLayoutAttribute#>,  指定view2的属性attr2
                         multiplier: <#CGFloat#>,  指定一个与view2属性相乘的乘数multiplier
                         constant: <#CGFloat#>))  指定一个与view2属性相加的浮点数constant

公式:view1.attr1 <relation> view2.attr2 * multiplier + constant

eg:
[NSLayoutConstraint constraintWithItem:view1
                             attribute:NSLayoutAttributeLeft
                             relatedBy:NSLayoutRelationEqual
                                toItem:view2
                             attribute:NSLayoutAttributeRight
                            multiplier:1
                              constant:10]
          代码意思是:    view1的左侧,在,view2的右侧,再多10个点,的地方。


NSLayoutRelation:
typedef NS_ENUM(NSInteger, NSLayoutRelation) {
    NSLayoutRelationLessThanOrEqual = -1,          //小于等于
    NSLayoutRelationEqual = 0,                     //等于
    NSLayoutRelationGreaterThanOrEqual = 1,        //大于等于
};

NSLayoutAttribute:
typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
     NSLayoutAttributeLeft = 1,                     //左侧
     NSLayoutAttributeRight,                        //右侧
     NSLayoutAttributeTop,                          //上方
     NSLayoutAttributeBottom,                       //下方
     NSLayoutAttributeLeading,                      //首部
     NSLayoutAttributeTrailing,                     //尾部
     NSLayoutAttributeWidth,                        //宽度
     NSLayoutAttributeHeight,                       //高度
     NSLayoutAttributeCenterX,                      //X轴中心
     NSLayoutAttributeCenterY,                      //Y轴中心
     NSLayoutAttributeBaseline,                     //文本底标线
     NSLayoutAttributeNotAnAttribute = 0            //没有属性
};

//水平约束NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[greenView]-20-[yellowView(==greenView)]-20-|", options: 0, metrics: nil, views: [view1 : view2])
//垂直约束   NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[greenView]-20-[yellowView(==greenView)]-20-|", options: 0, metrics: nil, views: [view1 : view2])

//修改约束
UIView.animateWithDuration(0.3, animations: { () -> Void in
     //有时需要
     superview.layoutIfNeeded()
     self.layoutIfNeeded()
     self.deleteLabel.layoutIfNeeded()
})

相关文章

  • swift - 给一个视图添加约束

  • tableviewCell 和约束的那些问题 2020-05-0

    tableviewcell 添加约束,约束需要添加到contenview 上面,否则就不显示子视图内容

  • UIScrollView+欢迎界面

    滚动视图(尽量少使用StoryBoard,如果使用了故事版添加滚动视图,在滚动视图里添加控件时不要拉约束) 1` ...

  • ConstraintLayout完全解析2

    调整约束偏差 向视图的两侧添加约束(并且同一维度的视图大小为“固定”或“换行内容”)时,视图将在两个约束之间居中,...

  • iOS Xib 实现UIScrolleView

    思路: 1.在xib中添加一个滚动视图UIScrollView,上下左右约束都为0。2.在滚动视图上添加一个UIV...

  • 6.3 添加edges/top/bottom/leading/t

    1. 本节课将为您演示,如何给位于滚动视图中的标签视图,添加约束关系。首先在左侧的项目导航区,打开视图控制器的代码...

  • iOS Masonry使用总结

    Masonry布局定位约束冲突 视图布局添加约束的时候,有的时候会报一对约束冲突:Probably at leas...

  • SnapKit的使用

    SnapKit 是约束库,即Masonry的swift版本 约束布局原理: 直接设置具体的值 代码如下 与父视图有...

  • iOS代码添加视图约束

    项目要做这样一个效果的启动页。 考虑到版本号是会不断变更的,因此采用动画效果启动页,让版本号动态加载iOS启动页动...

  • Swift 代码添加约束

    注意:view添加或更改约束之前需要将该view的属性translatesAutoresizingMaskInto...

网友评论

      本文标题:swift - 给一个视图添加约束

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