- (void)viewDidLoad {
[super viewDidLoad]; UIButton *btnTest = [UIButton buttonWithType:UIButtonTypeCustom];//不需要去刻意指定x,y的坐标,可以用CGRectZero
btnTest.backgroundColor = [UIColor redColor];
btnTest.layer.borderColor = [UIColor yellowColor].CGColor;
btnTest.layer.borderWidth = 2.0;
[self.view addSubview:btnTest]; [btnTest setTranslatesAutoresizingMaskIntoConstraints:NO];//将使用AutoLayout的方式布局
//btnTest顶部相对于self.view的顶部距离为100
NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:100];
//btnTest左侧相对于self.view的左侧距离为100
NSLayoutConstraint *constraintLeft = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:100];
//btnTest右侧相对于self.view的右侧距离为100
NSLayoutConstraint *constraintRight = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:btnTest attribute:NSLayoutAttributeRight multiplier:1.0 constant:100];
//btnTest底部相对于self.view的底部距离为100
NSLayoutConstraint *constraintBottom = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:btnTest attribute:NSLayoutAttributeBottom multiplier:1.0 constant:100];
//水平居中
NSLayoutConstraint *constraintXCenter = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:00.0f];
//垂直居中
NSLayoutConstraint *constraintYCenter = [NSLayoutConstraint constraintWithItem:btnTest attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:00.0f]; //将约束添加到父视图中
[self.view addConstraint:constraintTop];
[self.view addConstraint:constraintLeft];
[self.view addConstraint:constraintRight];
[self.view addConstraint:constraintBottom];
[self.view addConstraint:constraintXCenter];
[self.view addConstraint:constraintYCenter]; }

  

IOS手动添加的View 在代码中使用(自动布局)autoLayout的更多相关文章

  1. IOS 手动添加第三方库报错问题

    当不想使用CocoaPods来管理和使用第三方库的时候,就需要手动添加和配置这些第三方库,难免会出现一些问题,主要问题汇总如下: 1.AFNetworking.NKNetWork.ZxingObjC等 ...

  2. IOS手动添加CoreData

    手动添加coreData: 1.在target-build phrase-Link binary with libraries里增加CoreData Framework 2.加入数据模型:comman ...

  3. ios 手动添加mapview

    1,首先选中Build Phases ,在Link Binary With Libraries 下添加MapKit.framework框架 2,在头文件(.h文件)处添加:#import <Ma ...

  4. ios手动添加数组字典(NSMutableDictionary)

    @property (nonatomic,strong) NSArray *imageData;//定义一个数组 -(NSArray *)imageDate { if(_imageDate==nil) ...

  5. iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority【转】

    原文:http://www.mgenware.com/blog/?p=491 接上文:iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级. 我们继续来看在代码中使用Autola ...

  6. iOS:viewController 和 view 的生命周期、不错的代码设计风格

    一.介绍: viwe和viewController的生命周期是最基本的知识,如果很好地理解它们的方法调用的执行顺序,就能很好地设计代码的风格.这篇博客转载自:http://www.cnblogs.co ...

  7. 移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)

    移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签) 一.总结 一句话总结: 添加viewport标签:meta name="viewport" ...

  8. 仅仅需手动添加一行代码就可以让Laravel4执行在SAE (v. 1.0.0)

    Github:https://github.com/chariothy/laravel4-sae (已更新至v1.1.0) laravel4-sae (v. 1.0.0) 仅仅需手动添加一行代码就可以 ...

  9. mfc---手动给toolbar按钮添加消息View中

    手动给toolbar按钮添加消息View中: .h: afx_msg void OnButtonBG(); .cpp: ON_COMMAND(ID_BUTTON_BG,OnButtonBG) .cpp ...

随机推荐

  1. Yii框架的学习指南(策码秀才篇)1-2 一步步学习yii framework

    我也是新手,不过之前学习了tp和ci框架,所以尝鲜想看看yii到底有多强大. 如何新建一个webapp(网站应用)呢,下面是2个步骤: 1. cmd 切换到htdocs下面的yii目录下的framew ...

  2. 树形dp(最小支配集)

    http://poj.org/problem?id=3659 #include<iostream> #include<cstring> #include<algorith ...

  3. PCIe的事务传输层的处理(TLP)

    主要从以下几个方面解决: 1.TLP基本的概念: 2.寻址定位与路由导向 3.请求和响应机制 4.虚拟通道机制 5.数据完整性 6.i/o,memory,configuration,message r ...

  4. win10安装motionbuilder失败,怎么强力卸载删除注册表并重新安装

    一些搞设计的朋友在win10系统下安装motionbuilder失败或提示已安装,也有时候想重新安装motionbuilder的时候会出现本电脑windows系统已安装motionbuilder,你要 ...

  5. linux有些sh文件,为什么要用 ./ 来执行

    因为有环境变量PATH,里面包含了许多目录,这些目录下的可执行文件就无需输入完整路径来执行.你可以用 echo "$PATH"查看当前的环境变量包含的目录,自带的命令文件都是在PA ...

  6. Java - 面向对象练习 - market

    Marketpackage market; public class Market {    private String marname;    private Product[] producta ...

  7. 为什么有这么多人呼吁要取代Facebook?

    扎克伯格这段时间算是栽了,身为人生大赢家的他或许正在遭受最为灰暗的时刻.因为泄露5000万用户数据这一事件,Facebook股价暴跌且扎克伯格身家缩水.被英国与美国相关部门调查.#删除Facebook ...

  8. 吴裕雄--天生自然 PYTHON数据分析:基于Keras的CNN分析太空深处寻找系外行星数据

    #We import libraries for linear algebra, graphs, and evaluation of results import numpy as np import ...

  9. Laravel Study(使用 Laravel )

    開始 伺服器及相關工具安裝自行建立,在伺服器跟目錄下 有兩種方式建立 Laravel 專案,這裡使用 composer 建立專案 使用 composer 要在 PHP 5.3.2 以上才能使用 com ...

  10. 正则表达式之RegExp对象

    1.定义 RegExp对象是原生JavaScript中表示正则表达式的对象:是正则表达式(regular expression)的简写.有一点需要明确知道,正则表达式只能对字符串进行操作. 2.语法 ...