Swift开发:NSLayoutConstraint纯代码实现自动布局-初级篇
2016-05-04 18:35
501 查看
要求 宽高200的view,通过代码,使得view在距离父控件的右下角20边距处
/* 约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的
控件约束那么有父控件添加
*创建约束 NSLayoutConstraint 参数 说明:
* item 自己
* attribute
* relatedBy 大于等于 小于等于 等于
* toItem 另外一个控件
* attribute 另一个控件的熟悉
* multiplier 乘以多少
* constant : 加上多少
* NSLayoutConstraint : 某个控件的属性值 等于 另外一个控件的属性值
乘以多少 加上多少
* 添加约束 addConstraint
*/
swift 代码:
let blueView =UIView();
blueView.backgroundColor =UIColor.blueColor()
self.view.addSubview(blueView)//系统默认会给autoresizing
约束
// 关闭autoresizing 不关闭否则程序崩溃
blueView.translatesAutoresizingMaskIntoConstraints =false
//宽度约束
let width:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil,
attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)
blueView.addConstraint(width)//自己添加约束
//高度约束
let height:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil,
attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)
blueView.addConstraint(height)//自己添加约束
//右边约束
let right:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Right, relatedBy:NSLayoutRelation.Equal, toItem:self.view,
attribute:NSLayoutAttribute.Right, multiplier:1.0, constant: -20)
blueView.superview!.addConstraint(right)//父控件添加约束
//下边约束
let bottom:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Bottom, relatedBy:NSLayoutRelation.Equal, toItem:self.view,
attribute:NSLayoutAttribute.Bottom, multiplier:1.0, constant: -20)
blueView.superview!.addConstraint(bottom)//父控件添加约束
效果图:
/* 约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的
控件约束那么有父控件添加
*创建约束 NSLayoutConstraint 参数 说明:
* item 自己
* attribute
* relatedBy 大于等于 小于等于 等于
* toItem 另外一个控件
* attribute 另一个控件的熟悉
* multiplier 乘以多少
* constant : 加上多少
* NSLayoutConstraint : 某个控件的属性值 等于 另外一个控件的属性值
乘以多少 加上多少
* 添加约束 addConstraint
*/
swift 代码:
let blueView =UIView();
blueView.backgroundColor =UIColor.blueColor()
self.view.addSubview(blueView)//系统默认会给autoresizing
约束
// 关闭autoresizing 不关闭否则程序崩溃
blueView.translatesAutoresizingMaskIntoConstraints =false
//宽度约束
let width:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Width, relatedBy:NSLayoutRelation.Equal, toItem:nil,
attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)
blueView.addConstraint(width)//自己添加约束
//高度约束
let height:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil,
attribute: NSLayoutAttribute.NotAnAttribute, multiplier:0.0, constant:200)
blueView.addConstraint(height)//自己添加约束
//右边约束
let right:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Right, relatedBy:NSLayoutRelation.Equal, toItem:self.view,
attribute:NSLayoutAttribute.Right, multiplier:1.0, constant: -20)
blueView.superview!.addConstraint(right)//父控件添加约束
//下边约束
let bottom:NSLayoutConstraint =NSLayoutConstraint(item: blueView,
attribute: NSLayoutAttribute.Bottom, relatedBy:NSLayoutRelation.Equal, toItem:self.view,
attribute:NSLayoutAttribute.Bottom, multiplier:1.0, constant: -20)
blueView.superview!.addConstraint(bottom)//父控件添加约束
效果图:
相关文章推荐
- swift -UITextField
- swift 使用UIButton
- swift语言UILabel的使用
- Swift 中枚举高级用法及实践
- swift学习笔记(一)基本数据类型(下)
- Swift使用友盟分享
- [swift学习之二]基本语法练习
- OC中引用swift文件报错误的解决方法
- swift学习笔记(一)基本数据类型(上)
- Swift语法之 ---- ?和!区别
- swift中webview的URL带中文或者特殊字符的处理
- init?(coder aDecoder: NSCoder)
- 从Java/Android到Swift iOS开发:语言与框架对比
- Swift之枚举类型&结构体
- swift post json and header
- ipad版Qzone(纯swift编写)
- Google Android或采纳苹果的Swift编程语言:避开Java这个坑?
- swift中提供属性监听器,可以监听属性的改变
- swift设置UIButton的title文字
- 使用swift解析json