Autolayout自动布局3
来源:互联网 发布:联通有网络机顶盒吗 编辑:程序博客网 时间:2024/06/05 19:28
UIView *blueView = [[UIView alloc] init]; [self.view addSubview:blueView]; blueView.backgroundColor = [UIColor blueColor]; //不要将AutoresizingMask转为Autolayout的约束 blueView.translatesAutoresizingMaskIntoConstraints = NO; UIView *redView = [[UIView alloc] init]; [self.view addSubview:redView]; redView.backgroundColor = [UIColor redColor]; //不要将AutoresizingMask转为Autolayout的约束 redView.translatesAutoresizingMaskIntoConstraints = NO; /**************blueView的约束**************/ //添加高度约束:40 NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:40]; [blueView addConstraint:heightConstraint]; //添加左边约束:blueView的左边距离父控件右边有20的间距 NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20]; [blueView.superview addConstraint:leftConstraint]; //添加右边约束:blueView的右边距离父控件右边有20的间距 NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20]; [blueView.superview addConstraint:rightConstraint]; //添加顶部约束:blueView的顶部距离父控件的顶部有20的间距 NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]; [blueView.superview addConstraint:topConstraint]; /**************redView的约束**************/ //添加高度约束:跟blueView一致 NSLayoutConstraint *heightConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]; [self.view addConstraint:heightConstraint2]; //添加左边约束:redView左边 == 父控件的中心X NSLayoutConstraint *leftConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [self.view addConstraint:leftConstraint2]; //添加顶部约束:redView的顶部距离blueView有20的间距 NSLayoutConstraint *topConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20]; [blueView.superview addConstraint:topConstraint2]; //添加右边约束:redView的右边 == blueView的右边 NSLayoutConstraint *rightConstraint2 = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]; [self.view addConstraint:rightConstraint2];
0 0
- Autolayout自动布局3
- autolayout自动布局详解
- AutoLayout(自动布局)
- AutoLayout 自动布局
- ios Autolayout自动布局
- autoLayout 自动布局
- AutoLayout自动布局
- Autolayout(自动布局)
- 适配 - autolayout自动布局
- autoLayout-自动布局
- iOS AutoLayout 自动布局
- AutoLayout (自动布局)
- Autolayout自动布局1
- Autolayout自动布局2
- AutoLayout 自动布局
- Autolayout 自动布局
- Autolayout 自动布局
- 自动布局:AutoLayout注意事项及autolayout动画
- HashMap原理解析
- ORACLE表空间和表碎片分析及整理方法
- struts请求走向流程
- Android动态加载技术 简单易懂的介绍方式
- LeetCode70. Climbing Stairs
- Autolayout自动布局3
- 图片加载框架之Picasso
- Python异常处理
- Power of Three
- Android基础之Touch事件和手势处理
- Projects:Python项目大集合
- MonoDevelop 打开App异常问题
- HDU1284 【DP入门之完全背包】
- 1026. 程序运行时间(15)