zoukankan      html  css  js  c++  java
  • Swift

    NSLayoutConstraint参数说明:

    /**
     * 创建约束 NSLayoutConstraint  参数 说明:
     * item 自己
     * attribute
     * relatedBy 大于等于 小于等于 等于 ...
     * toItem 另外一个控件
     * attribute 另一个控件的属性
     * multiplier 乘以多少
     * constant : 加上多少
     * NSLayoutConstraint : 某个控件的属性值 等于 另外一个控件的属性值 
                    乘以多少 加上多少
     * 添加约束 addConstraint
     */

    NSLayoutConstraint(item: AnyObject, attribute: NSLayoutAttribute, relatedBy: NSLayoutRelation, toItem: AnyObject?, attribute: NSLayoutAttribute, multiplier: CGFloat, constant: CGFloat)

     

    创建子控件

    let childView = UILabel()
    childView.text = "正在链接服务器"
    childView.backgroundColor = UIColor.yellow //背影黄色
    childView.textAlignment = .center //文本居中对齐
    
    parentView.backgroundColor = UIColor.red//设置父元素背影红色,方便查看效果

    关闭autoresizing

    // 关闭autoresizing,不然无效果
    childView.translatesAutoresizingMaskIntoConstraints =false

    创建约束:

    //创建相对父view的左右间距各20
    //左约束
    let leftConstraint = NSLayoutConstraint(item: childView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: topStatusBarView, attribute:NSLayoutAttribute.leading, multiplier: 1.0, constant: 20)
    
    //右约束
    let rightConstraint = NSLayoutConstraint(item: childView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: topStatusBarView, attribute:NSLayoutAttribute.trailing, multiplier: 1.0, constant: -20)
    
    let centerConstraint = NSLayoutConstraint(item: childView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: topStatusBarView, attribute:NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0)
    
    //子控件自身高度
    let heigtConstraint = NSLayoutConstraint(item: childView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute:NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 25)
    
    //子控件内部自己的属性(高度,自己添加)      
    childView.addConstraint(heigtConstraint)
    
    //子元素相对父亲的元素,由父添加
    parentView.addConstraints([leftConstraint, rightConstraint, centerConstraint])

    例:

    Swift2.3

    let imgCoupon = UIImageView()
    imgCoupon.frame = CGRectMake(0, 0, 16, 16)
    imgCoupon.loadImgByNamed("coupon")
    imgCoupon.tag = 12345
    cell.addSubview(imgCoupon)
    imgCoupon.translatesAutoresizingMaskIntoConstraints = false
    let l = NSLayoutConstraint(item: imgCoupon, attribute: .Leading, relatedBy: .Equal, 
        toItem: firstView, attribute: .Trailing, multiplier: 1, constant: 3)
    l.active = true
    let c = NSLayoutConstraint(item: imgCoupon, attribute: .CenterY, relatedBy: .Equal,
         toItem: lbName, attribute: .CenterY, multiplier: 1, constant: 0)
    c.active = true 
    let h = NSLayoutConstraint(item: imgCoupon, attribute: .Height, relatedBy: .Equal, 
        toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 16)
     h.active = true
    
    let w = NSLayoutConstraint(item: imgCoupon, attribute: .Width, relatedBy: .Equal, 
        toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 16)
    w.active = true

    Swfit2.3中使用w.active = true 而Swift3.0中是addConstraint(w),并且attribute的值也不同

    PS:苹果官网API - NSLayoutConstraint

  • 相关阅读:
    机器学习入门实践——线性回归&非线性回归&mnist手写体识别
    基于OpenCV的摄像头采集印刷体数字识别
    使用rviz实现本地计算机绘制机器人路径
    从0开始的FreeRTOS(4)
    从0开始的FreeRTOS(3)
    从0开始的FreeRTOS(2)
    从0开始的FreeRTOS(1)
    Robomaster电控入门(8)RTOS
    Robomaster电控入门(7)双轴云台控制
    计算机与网络课程设计开发纪要
  • 原文地址:https://www.cnblogs.com/gongyuhonglou/p/10311544.html
Copyright © 2011-2022 走看看