参考资料
开源项目Masonry旨在让自动布局(Auto Layout)的代码更简洁、可读性更强,下面是它的地址,个人认为在开发过程中使用Masonry可以是代码更加友好
 
- (void)viewDidLoad
{
[super viewDidLoad]; [self addNavigationBar:FEEDBACK_VIEW_CONTROLLER]; UILabel *note = [[UILabel alloc] init];
[note setText:@"欢迎提出宝贵意见!您留下的每一份心意都将浇灌母婴宝的茁壮成长。"];
[note setLineBreakMode:NSLineBreakByWordWrapping];
note.numberOfLines = 0;
[self.view addSubview:note]; //将自适应向布局约束的转化关掉(根据情况有时需要有时不需要)
[note setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:note
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:navBar
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:10]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:note
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:note
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10]];
UITextView *tfContent = [[UITextView alloc] init];
[tfContent setTranslatesAutoresizingMaskIntoConstraints:NO];
[tfContent setBackgroundColor:[UIColor whiteColor]];
[tfContent setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:tfContent]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:note
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100]]; UITextField *tfEmail = [UITextField new];
[tfEmail setTranslatesAutoresizingMaskIntoConstraints:NO];
[tfEmail setBackgroundColor:[UIColor whiteColor]];
[tfEmail setClearButtonMode:UITextFieldViewModeAlways];
[tfEmail setReturnKeyType:UIReturnKeyDone];
[tfEmail setPlaceholder:@"请输入邮箱,以便我们联系你!"];
[self.view addSubview:tfEmail]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:tfContent
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:35]]; UIButton *btnSubmit = [[UIButton alloc] init];
[btnSubmit setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnSubmit setTitle:STRING_COMMON_SUBMIT forState:UIControlStateNormal];
[btnSubmit setBackgroundImage:[UIImage imageNamed:@"bg_red_orange"] forState:UIControlStateNormal];
[btnSubmit setBackgroundImage:[UIImage imageNamed:@"bg_red"] forState:UIControlStateHighlighted];
[self.view addSubview:btnSubmit]; NSMutableArray *tmpConstraints = [NSMutableArray array]; [tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tfEmail]-10-[btnSubmit(==35)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tfEmail,btnSubmit)]];
[tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[btnSubmit]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnSubmit)]];
[self.view addConstraints:tmpConstraints]; }

  

IOS自动布局的更多相关文章

  1. iOS 自动布局详细介绍

    1. 自动布局的理解 iOS自动布局很有用,可以在不同size的屏幕上运行,原先看的头痛,还是习惯用最蠢的[UIScreen mainScreen].bounds.size.width等来布局,后来实 ...

  2. iOS自动布局——Masonry详解

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由鹅厂新鲜事儿发表于云+社区专栏 作者:oceanlong | 腾讯 移动客户端开发工程师 前言 UI布局是整个前端体系里不可或缺的一环 ...

  3. 掌握iOS自动布局

    1,自动布局是一种基于约束的布局(constraint-based layout)引擎,它可以根据开发者在对象上的约束自动调整大小与位置. 2.在iOS 6之前使用的布局模型是“spring& ...

  4. iOS自动布局之autoresizingi

    对于iOS的app开发者来说,不会像Android开发者一样为很多的屏幕尺寸来做界面适配,因此硬编码的坐标也能工作良好,但是从设计模式上来说这不是好的做法.而且也还有一些问题,如iPhone5的适配, ...

  5. 学会爱上iOS自动布局(Auto Layout) - 剑尖

    本文翻译自Yari Dareglia的LEARN TO LOVE AUTO LAYOUT文章先生们,女士们,让我们以正确的心态开始本教程吧:自动布局就是简单!我花了一段时间来掌握自动布局是如何工作的, ...

  6. iOS 自动布局过程

    自动布局将视图显示在屏幕上的步骤: 更新约束,它会从子视图传递到父视图,设置布局信息:约束更新时自动触发setNeedsUpdateConstraints:updateConstraints可以增加本 ...

  7. iOS 自动布局框架 – Masonry 详解

    目前iOS开发中大多数页面都已经开始使用Interface Builder的方式进行UI开发了,但是在一些变化比较复杂的页面,还是需要通过代码来进行UI开发的.而且有很多比较老的项目,本身就还在采用纯 ...

  8. iOS自动布局框架-Masonry详解

    首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout     从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...

  9. IOS 自动布局-UIStackPanel和UIGridPanel(四)

    为什么说scrollview的自动化布局是难点? 对scrollview做自动化布局,无非就是想对scrollview里面的subviews来做自动化布局.但是scrollview里面的subview ...

随机推荐

  1. 18.allegro区域约束规则设置

    一.线宽和线间距 --- ---------------- 然后再电路板上创建一个区域 ----- ---- --- ---- ------------------------------

  2. leetcode:Add Two Numbers

    题目描述:You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  3. python webdriver测试报告

    python webdriver测试报告 即将开始一系列的自动化项目实践,很多公共类和属性都需要提前搞定.今天,解决了测试报告的一些难题,参照了很多博文,最终觉得HTMLTestRunner非常不错, ...

  4. Photoshop图层混合模式计算公式大全(转)

    混合模式可以将两个图层的色彩值紧密结合在一起,从而创造出大量的效果.在这些效果的背后实际是一些简单的数学公式在起作用.下面我将介绍photoshop cs2中所有混合模式的数学计算公式.另外还介绍了不 ...

  5. ios多手势事件

    开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollVi ...

  6. hadoop数据容易出现错误的地方

    最近在搞关于数据分析的项目,做了一点总结. 下图是系统的数据流向.容易出现错误的地方.1.数据进入hadoop仓库有四种来源,这四种是最基本的数据,简称ods,original data source ...

  7. 一些纯css3写的公司logo

      随着对css3了解得越深入,越来越发现了css3的强大.css3不但能完成一些基本的特效如圆角阴影等,还能借助动画技术实现一些复杂的动画,能替代很多以前js才能完成的工作,css3的作用还不止于此 ...

  8. setAttribute一个兼容性问题

    前几天工作中遇到一个js问题,本来js就不大会,倒腾了好长时间,并在做弹窗的时候用到了setAttribute,出现了不兼容的问题,在网上查了好多,真是郁闷,看来啥都得学啊. 主要的工作是做一个根据时 ...

  9. mysqldump备份数据库时排除某些库

    说明:使用mysqldump –all-databases会导出所有库.但如果做主从,从主库dump出数据时,我们是不需要也不想要information_schema 和 mysql 库的.数据库少的 ...

  10. ios ble 参考

    About Core Bluetooth https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Con ...