NSLayoutConstraint 的系统约束纯代码模式swift
来源:互联网 发布:电视直播软件哪个好 编辑:程序博客网 时间:2024/05/16 10:30
//之前用storyboard拉过约束觉得特别好用,但是并没有改变我纯代码开发的梦想所以简单的写了个例子
//推荐看官方文档详细全面
//一般约束为4个或者以上(label比较特殊最少2个就够了不用设置其宽高 它会根据字体来调整)所以一般代码约束是当场看不出来对错的写的时候要谨慎
//下面展示个简单的swift的例子 等熟悉了 会写一个用约束来实现label根据内容变化大小的例子
import UIKit
class ViewController: UIViewController {
//定义一个高度的约束
var height:NSLayoutConstraint!
overridefunc viewDidLoad() {
super.viewDidLoad()
buildControl()
// Do any additional setup after loading the view, typically from a nib.
}
/**
界面拖了一个button链接的点击事件
- parameter sender: button
*/
@IBActionfunc btnClick(sender: AnyObject) {
//点击按钮改变高度约束
self.height.constant =200
}
//创建界面、
func buildControl()
{
//创建一个view
let segementPage =UIView()
segementPage.translatesAutoresizingMaskIntoConstraints =false
segementPage.backgroundColor =UIColor.orangeColor()
//这边要先添加到视图然后才可以添加约束
self.view.addSubview(segementPage)
//左上右的约束
let leftC =NSLayoutConstraint.init(item: segementPage, attribute:NSLayoutAttribute.Leading, relatedBy:NSLayoutRelation.Equal, toItem:self.view, attribute:NSLayoutAttribute.Leading,multiplier:1, constant: 0)as NSLayoutConstraint
let topC =NSLayoutConstraint.init(item: segementPage, attribute:NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem:self.view, attribute:NSLayoutAttribute.Top, multiplier:1, constant: 0)as NSLayoutConstraint
let rightC =NSLayoutConstraint.init(item: segementPage, attribute:NSLayoutAttribute.Trailing, relatedBy:NSLayoutRelation.Equal, toItem:self.view, attribute:NSLayoutAttribute.Trailing, multiplier:1, constant: 0)as NSLayoutConstraint
//这边是高度的约束因为没有参照物所以写法有点不同注意一下
let heightC =NSLayoutConstraint.init(item: segementPage, attribute:NSLayoutAttribute.Height, relatedBy:NSLayoutRelation.Equal, toItem:nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier:1, constant: 60)as NSLayoutConstraint
//新版的约束添加约束方法建议使用
// NSLayoutConstraint.activateConstraints([leftC,topC,rightC,heightC])
//单个约束可以用这个
// leftC.active = true
//老版的约束方法
//除了自己的属性高宽等其它属性(与父视图有交集的地方)应该添加在父视图上
self.view.addConstraints([leftC,topC,rightC])
//设置高度
segementPage.addConstraints([heightC])
//属性外设置来控制view的高度
self.height = heightC
}
overridefunc didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
- NSLayoutConstraint 的系统约束纯代码模式swift
- iOS-NSLayoutConstraint(代码约束)
- Swift NSLayoutConstraint 加约束Constraint
- 代码中添加约束(NSLayoutConstraint)
- NSLayoutConstraint 使用代码实现约束的添加和删除
- Swift开发:NSLayoutConstraint纯代码实现自动布局-初级篇
- 通过代码添加约束之NSLayoutConstraint
- 77.代码中添加约束(NSLayoutConstraint)
- NSLayoutConstraint约束
- Swift代码动态创建NSLayoutConstraint布局
- 纯代码 scrollview约束
- 纯代码讲解swift的指针
- swift纯代码UITabBarController的使用
- swift中NSLayoutConstraint布局的使用
- Swift 代码添加约束
- 纯代码约束使用AoutLayout
- NSLayoutConstraint万能约束公式
- 自定义NSLayoutConstraint约束库
- Zend Studio使用教程之集成Apigility(三)
- jsp过滤器Filter配置过滤器汇总
- C/C++程序员应聘常见面试题深入剖析
- UVA 11149 Power of Matrix(矩阵+二分)
- 数据结构的概念
- NSLayoutConstraint 的系统约束纯代码模式swift
- 进程间通信之管道
- 树莓派界面音频播放器 Qmmp
- 【Codeforces Round 362 (Div 2)E】【公式推导+快速幂+费马小定理】PLEASE a[i]=(1-a[i-1])除2下n次项 n为连乘数
- Criteria关联查询
- 关于微信公众号平台的“暂时无法提供服务”
- 字符串编辑距离
- hdu 1856 More is better (并查集)
- Android 调用系统方法调节音量