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则启 ...
随机推荐
- 简明 VIM 练级攻略
http://coolshell.cn/articles/5426.html vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个 ...
- Autofac 依赖注入
介绍 Autofac是一款IOC框架,很轻量级性能非常高,自动注入很给力. NuGet Autofac:Autofac控制反转容器核心 Autofac.MVC5:提供IDependencyResolv ...
- git 提交代码
git config --global user.name=a_name git config --global user.email=an_email_address mkdir test cd t ...
- apche启动错误|httpd.pid overwritten — Unclean shutdown of previous Apache run?
APACHE启动成功,但无法接受任何请求,查看ERROR.LOG文件[warn] pid file /opt/apache/logs/httpd.pid overwritten - Unclean s ...
- Python学习总结 03 Plotly 学习总结
一 Plotly 简介 Plotly是另一个免费进行数据分析和绘制图表的APP,建立在d3.js上. Plotly图可下载为SVG,EPS或PNG格式,并简单地导入到Illustrator或者Phot ...
- php中双冒号::的用法
注:本篇博客系转载,出处不可考(至少对我来说不可考...) 双冒号操作符即作用域限定操作符Scope Resolution Operator可以访问静态.const和类中重写的属性与方法. 在类定义外 ...
- runtime-对成员变量操作应用之归档和返归档
为了实现归档和返归档,我们要让被归档对象的类接受NSCoding协议并且实现协议里的两个方法 - (void)encodeWithCoder:(NSCoder *)aCoder; - (nullabl ...
- 如何更改gnome-screenshot的默认的保存路径?
参考这篇文章: http://www.itye.org/archives/3121 首先, 在dconf-editor中设置, screenshot的自动保存路径: auto-save-directo ...
- [Machine Learning & Algorithm] 神经网络基础
目前,深度学习(Deep Learning,简称DL)在算法领域可谓是大红大紫,现在不只是互联网.人工智能,生活中的各大领域都能反映出深度学习引领的巨大变革.要学习深度学习,那么首先要熟悉神经网络(N ...
- Javascript设计模式学习二(单例)
定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点 普通的单例模式: 使用一个变量来标记当前是否已经为某个类创建过对象,如果是的话,在下一次获取该类的实例时,直接返回之前创建的对象.比如:使用 ...