- (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. FaceIDViewer.rar

    FaceIDViewer用于查看Office工具栏控件的内置图标编号. 压缩包中包含4个文件,打开任何一个都可以进行查询.其中带有V2字样的文件,已经排除掉了空白图标. Excel版动态图: Word ...

  2. fidder 抓包工具设置只拦截指定ip(服务ip)

    直接上图:

  3. 调用支付JSAPI缺少参数:sign

    微信jsapi调用微信支付时报 :调用支付JSAPI缺少参数:sign 这个问题我查了很久,最后把前端的代码也拿过来看来,发现前端 sign的参数名传错了~~~~~~~前后端分离很容易出现这种问题,对 ...

  4. [LC] 434. Number of Segments in a String

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

  5. 吴裕雄--天生自然 JAVASCRIPT开发学习:对象 实例(3)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. EmguCV C# 安装入门教程

    EmguCv3的安装. EmguCv3下载网址 http://sourceforge.net/projects/emgucv/files/emgucv/3.0.0/ 推荐下载第一个: 点击direct ...

  7. C++头文件和std命名空间

    C++ 是在C语言的基础上开发的,早期的 C++ 还不完善,不支持命名空间,没有自己的编译器,而是将 C++ 代码翻译成C代码,再通过C编译器完成编译.这个时候的 C++ 仍然在使用C语言的库,std ...

  8. 前端-css-长期维护

    ###############    CSS简介    ################ # CSS # HTML是骨架 # CSS是样式 # JS是动作 # css和html是分成两个文件编写的,这 ...

  9. deeplearning.ai 神经网络和深度学习 week1 深度学习概论

    1. 预测房价.广告点击率:典型的神经网络,standard NN. 图像:卷积神经网络,CNN. 一维序列数据,如音频,翻译:循环神经网络,RNN. 无人驾驶,涉及到图像.雷达等更多的数据类型:混合 ...

  10. OpenCV 改变图像的对比度和亮度

    #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <ios ...