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

 

Swift - 代码创建NSLayoutConstraint布局的更多相关文章

  1. 【Android】纯代码创建页面布局(含异步加载图片)

    开发环境:macOS 10.12 + Android Studio 2.2,MinSDK Android 5.1 先看看总体效果 本示例是基于Fragment进行的,直接上代码: [界面结构] 在 F ...

  2. Swift - 代码创建单例

    创建单例的方法 import UIKit //创建一个单例类 class SingleInstance: NSObject { //在单例类中,有一个用来共享数据的数组 var datas = [St ...

  3. swift - 代码创建 pickerView 显示或隐藏横线

    import UIKit class VC1: UIViewController { fileprivate lazy var pickerV : UIPickerView = { let v = U ...

  4. Swift开发:NSLayoutConstraint纯代码实现自动布局-初级篇

    要求 宽高200的view,通过代码,使得view在距离父控件的右下角20边距处 /* 约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的 控件约束那么有父控件添加 *创建约束 NSLayo ...

  5. swift 字符转为类,代码创建控件

    在使用类之前要先获得 命名空间 通过json来获取 字符型的类名 然后创建类对象,这时候就要用到字符转类 // 从info字典中获取到 命名空间 转为字符型 let NS = NSBundle.mai ...

  6. iOS Swift 开发语言之初接触,纯代码创建UIView,UITableView,UICollectionView

    1. 初始化Label设置AttributeString override func viewDidLoad() { let label = UILabel(frame:CGRect(x:,y:,wi ...

  7. swift:创建集合视图UICollectionView

    swift中创建集合视图和OC中差不多,主要是实现UICollectionViewDataSource数据源协议和UICollectionViewDelegateFlowLayout自定义布局协议,其 ...

  8. 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码

    在一个应用的整个开发过程中涉及到了无数的步骤.其中一些是应用的说明,图片的创作,应用的实现,和实现过后的测试阶段.写代码可能组成了这个过程的绝大部分,因为正是它给了应用生命,但是这样还不够,与它同等重 ...

  9. ios - 纯代码创建collectionView

    开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...

随机推荐

  1. CentOS 7 防火墙 firewall 的使用

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disabl ...

  2. 论php数组合并

    注:尽量不要在循环中操作数据库. 1.两个一维数组合并成一个一维数组 $a = array('morning','afternoon','night'); $b = array('breakfast' ...

  3. UOJ59 WC2013 小Q运动季

    题意:给一个模线性方程组,构造解满足尽量多的方程. 直接枚举. 模数两两互质,先排除无解的,然后CRT,然而要高精,上python. 直接高消,有完美解. 2^20暴搜. 分解模数,对每个质数高消,C ...

  4. 【原创】PageAdminCMS 前台SQL注入漏洞(2)

    之前根据公司的要求找了几个web程序的漏洞提交CNVVD,发现漏洞提交上去两个月了,CNVVD却没有任何回应,我提交的这几个漏洞却悄悄的修补掉了. 文章作者:rebeyond 受影响版本:V3.0 漏 ...

  5. IntelliJ IDEA 15,16 win 7 64位安装包以及注册码 百度云盘

    不知道发出来,有用没有,要是官网下载不了的话,可以用我的这个哦,虽然不是最新的. ideaIU-162.1447.21 ideaIU-15.0.2 win7  64系统的安装包 链接:http://p ...

  6. [原创] Delphi小工具(Windows资源管理器右键菜单扩展)

    两个小工具 1. 项目临时文件清理 2. Android Ndk 编译 c/c++ jni 源码工具. 下载后,点击Reg.bat就可以完成注册安装.不需要时点击 UnReg.Bat 就可以删除菜单. ...

  7. DX 系列之 ComboBoxEdit

    参考资料: ComboBoxEdit.Properties.Items=ComboBoxItemCollection

  8. eclipse SVN Android项目出错,程序老闪退

    百度得知是.so文件被SVN客户端默认为是链接后的文件,认为是不提交的.因此需要将.so文件手动添加到版本控制.

  9. 各大浏览器内核介绍(Rendering Engine)

    在介绍各大浏览器的内核之前,我们先来了解一下什么是浏览器内核. 所谓浏览器内核就是指浏览器最重要或者说核心的部分"Rendering Engine",译为"渲染引擎&qu ...

  10. jemter的安装和使用

    1.下载地址:http://jmeter.apache.org/download_jmeter.cgi 2.安装 (1)解压,解压之后如图 (2)配置环境变量 我解压之后的目录是:D:\softwar ...