Swift - 代码创建NSLayoutConstraint布局
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布局的更多相关文章
- 【Android】纯代码创建页面布局(含异步加载图片)
开发环境:macOS 10.12 + Android Studio 2.2,MinSDK Android 5.1 先看看总体效果 本示例是基于Fragment进行的,直接上代码: [界面结构] 在 F ...
- Swift - 代码创建单例
创建单例的方法 import UIKit //创建一个单例类 class SingleInstance: NSObject { //在单例类中,有一个用来共享数据的数组 var datas = [St ...
- swift - 代码创建 pickerView 显示或隐藏横线
import UIKit class VC1: UIViewController { fileprivate lazy var pickerV : UIPickerView = { let v = U ...
- Swift开发:NSLayoutConstraint纯代码实现自动布局-初级篇
要求 宽高200的view,通过代码,使得view在距离父控件的右下角20边距处 /* 约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的 控件约束那么有父控件添加 *创建约束 NSLayo ...
- swift 字符转为类,代码创建控件
在使用类之前要先获得 命名空间 通过json来获取 字符型的类名 然后创建类对象,这时候就要用到字符转类 // 从info字典中获取到 命名空间 转为字符型 let NS = NSBundle.mai ...
- iOS Swift 开发语言之初接触,纯代码创建UIView,UITableView,UICollectionView
1. 初始化Label设置AttributeString override func viewDidLoad() { let label = UILabel(frame:CGRect(x:,y:,wi ...
- swift:创建集合视图UICollectionView
swift中创建集合视图和OC中差不多,主要是实现UICollectionViewDataSource数据源协议和UICollectionViewDelegateFlowLayout自定义布局协议,其 ...
- 使用Xcode HeaderDoc和Doxygen文档化你的Objective-C和Swift代码
在一个应用的整个开发过程中涉及到了无数的步骤.其中一些是应用的说明,图片的创作,应用的实现,和实现过后的测试阶段.写代码可能组成了这个过程的绝大部分,因为正是它给了应用生命,但是这样还不够,与它同等重 ...
- ios - 纯代码创建collectionView
开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...
随机推荐
- Python Day15
JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...
- 19个必须知道的Visual Studio快捷键
项目相关的快捷键 Ctrl + Shift + B = 生成项目 Ctrl + Alt + L = 显示Solution Explorer(解决方案资源管理器) Shift + Alt+ C = 添加 ...
- 使用rsync+inotify同步两台服务器文件
目标功能:将B服务器文件同步到A服务器 A服务器rsyncd.conf配置 权限600 A服务器rsyncd.pas文件配置 权限600 同步文件路径 /data/wwwroot/shen/ 权限 ...
- python版本随意切换之python2.7+django1.8.7+uwsgi+nginx源码包部署。
资源准备: wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz wget https://www.djangoproject ...
- swift3.0变化总结
Swift 3.0 做出的改变很大,在这篇文章中,我将尽我所能,利用代码样例给大家解释Swift 3.0最重要(要命)的改变,希望大家能够做好升级Swift 3.0 的准备.Swift 3.0的改变不 ...
- MATLAB-RSP 随笔
1.Getting Started with Signal Analyzer App 161216 (1) 在命令行中输入:signalAnalyzer. (2)在工作空间中选择要分析的信号
- DNA解链统计物理
来源:Kerson Huang, Lectures on Statistical Physics and Protein Folding, pp 24-25 把双链DNA解开就像拉拉链.设DNA有\( ...
- xml解析技术
本文总结Dom,sax解析, 使用Java作为工具解析xml文档. 1 Dom 综述:Dom解析xml通常也称为xmlDom (和htmlDom技术差不多),将xml文档封装成树,好处就是xml中的 ...
- 【学习笔记】Struts2 应用开发步骤
1.在web.xml文件中定义核心Filter来拦截用户请求. <!-- 定义Struts 2的核心Filter --> <filter> <filter-name> ...
- Django Restful Framework (二): ModelSerializer
时常,你需要对django model 的实例进行序列化.ModelSerializer 类提供了一个捷径让你可以根据 Model 来创建 Serializer. ModelSerializer 类和 ...