iOS代码添加视图约束
项目要做这样一个效果的启动页。
![](http://upload-images.jianshu.io/upload_images/1727123-31c451233d63c556.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
考虑到版本号是会不断变更的,因此采用动画效果启动页,让版本号动态加载iOS启动页动画效果 - 简书
考虑到屏幕适配问题,因此采用代码对视图添加约束。在添加约束的过程中遇到了一些问题,在此做一下记录和总结.
代码实现autolayout的注意点:
1.要先禁止autoresizing功能,设置view 的translatesAutoresizingMaskIntoConstraints 属性为 NO;
2.添加约束之前,一定要保证相关控件都已经在各自的父控件上。(就是要先addsubview,然后再添加约束addConstraint:)
3.不用再给view设置frame
先上代码
welcome.h(创建一个继承自UIView的welcome类)
#import@interface welcome :UIView
+ (instancetype)startView;
- (instancetype) initWithBgImage:(UIImage *)bgImage;
- (void) startAnimation;
@end
welcome.m
- (instancetype) initWithBgImage:(UIImage *)bgImage{
if (self= [super initWithFrame:[UIApplication sharedApplication].keyWindow.bounds]) {
self.backgroundColor = [UIColor whiteColor];
// _imageView = [[UIImageView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];//不用再给view设置frame
_imageView = [[UIImageView alloc]init];
_imageView.image = bgImage;
[self addSubview:_imageView];//先,保证控件已经在父控件上
[self addPicConstraint];//后
_lab = [[UILabel alloc]init];
_lab.font = [UIFont systemFontOfSize:20];
_lab.textColor = [UIColor colorWithRed:46.0/255 green:168.0/255 blue:23.0/255 alpha:1];//注意:有小数点
_lab.textAlignment = NSTextAlignmentCenter;
_lab.text = @"清新空气,净化生活";
[self addSubview:_lab];
[self addLabConstraint];
......
}
给图片添加约束
- (void)addPicConstraint{
//禁用antoresizing
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
//创建约束
//添加高约束
NSLayoutConstraint *picHeight = [NSLayoutConstraint
constraintWithItem:_imageView attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual toItem:nil
attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:70];[_imageView addConstraint:picHeight];
//添加宽约束
NSLayoutConstraint *picWeight = [NSLayoutConstraint
constraintWithItem:_imageView attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:nil
attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:70];[_imageView addConstraint:picWeight];
//添加y方向约束
NSLayoutConstraint *picTop = [NSLayoutConstraint
constraintWithItem:_imageView attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:_imageView.superview
attribute:NSLayoutAttributeTop multiplier:1.0 constant:60];[self
addConstraint:picTop];//添加x方向约束
NSLayoutConstraint *picVer = [NSLayoutConstraint
constraintWithItem:_imageView attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual toItem:_imageView.superview
attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];[self addConstraint:picVer];
}
留意上面添加xy方向约束的代码,一开始的时候我是这样写的,以y方向为例
NSLayoutConstraint
*picTop = [NSLayoutConstraint constraintWithItem:_bgImageView
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:_bgImageView.superview attribute:NSLayoutAttributeTop
multiplier:1.0 constant:0];
[_bgImageView addConstraint:picTop];
代码运行的时候崩溃
The view hierarchy is not prepared for the constraint:When added to a
view, the constraint's items must be descendants of that view (or the
view itself). This will crash if the constraint needs to be resolved
before the view hierarchy is assembled. Break on
-[UIView(UIConstraintBasedLayout)
_viewHierarchyUnpreparedForConstraint:] to debug.2016-03-16 14:03:57.250 AirClean[22640:516239] View hierarchy unprepared for constraint.
![](http://upload-images.jianshu.io/upload_images/1727123-9767f7f2a1f46f4a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
想了很久也不知道是什么原因。最后再stackoverflow上找到了解决方法ios - Centering subview's X in autolayout throws "not prepared for the constraint" - Stack Overflow
崩溃的原因就是约束没添加到正确的地方。
什么意思呢?看下面的例子
![](http://upload-images.jianshu.io/upload_images/1727123-fcbba42ce3765302.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
使用storyboard时,我在一个父view上添加了一个橙色的子view。并给它添加了宽高的约束(为固定值)以及相对父view上边距、左边距的约束。从截图中看到,宽高的约束是添加在子控件自己身上的,因为它不依赖于别的控件。而xy方向的约束,则是添加在父控件上。所以上面代码,把相对于父控件的y方向的约束添加到子控件身上,这是不对的,必然会报错。
约束添加规则总结:
1.约束不依赖于其他控件(添加的约束和其他控件没有关系),会添加到自己身上
2.如果是父子关系,设置子控件的约束,约束添加到父控件上
3.如果是兄弟关系,设置两兄弟的约束,约束会添加到第一个共同的父控件上
ps:另外还有一个要注意的地方,用代码给UILable的文字设置颜色,一开始的时候出现了[UIColor colorWithRed: green: blue: alpha:] 失效问题。
网上搜索了一下,发现了问题的所在:RGB的颜色值范围都是在0.0~1.0之间的,并不是我们误认为的0~255。
![](http://upload-images.jianshu.io/upload_images/1727123-53c8f02b8ed5c2ce.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
正确的用法:_lab.textColor = [UIColor colorWithRed:46.0/255
green:168.0/255 blue:23.0/255
alpha:1.0];而且要注意上面四个参数都是float类型的(所以不能写成46/255)
iOS代码添加视图约束的更多相关文章
- iOS 11开发教程(十四)iOS11应用代码添加视图
iOS 11开发教程(十四)iOS11应用代码添加视图 如果开发者想要使用代码为主视图添加视图,该怎么办呢.以下将为开发者解决这一问题.要使用代码为主视图添加视图需要实现3个步骤. (1)实例化视图对 ...
- 利用代码添加autolayout约束
1.概述 通常我们通过storyboard能够完成的,代码也能够完成,所以这里介绍下代码实现约束的添加,通常我们不这么干(在不使用第三方框架的情况下,使用系统自带的类添加约束特别繁琐),所以这里仅仅简 ...
- iOS 11开发教程(十八)iOS11应用视图之使用代码添加按钮
iOS 11开发教程(十八)iOS11应用视图之使用代码添加按钮 由于使用编辑界面添加视图的方式比较简单,所以不在介绍.这里,直接讲解代码中如何添加.使用代码为主视图添加一个按钮的方式和在1.3.3节 ...
- iOS 11开发教程(十三)iOS11应用编辑界面添加视图
iOS 11开发教程(十三)iOS11应用编辑界面添加视图 在iOS中添加视图的方式有两种:一种是使用编辑界面添加视图:另一种是使用代码添加视图.以下是这两个方式的详细介绍. 1.编辑界面添加视图 使 ...
- iOS 9应用开发教程之使用代码添加按钮美化按钮
iOS 9应用开发教程之使用代码添加按钮美化按钮 丰富的用户界面 在iOS9中提供了很多的控件以及视图来丰富用户界面,对于这些视图以及控件我们在上一章中做了简单的介绍.本章我们将详细讲解这些视图. i ...
- 【转】iOS学习之Autolayout(代码添加约束) -- 不错不错
原文网址:http://www.cnblogs.com/HypeCheng/articles/4192154.html DECEMBER 07, 2013 学习资料 文章 Beginning Auto ...
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
http://blog.csdn.net/pucker/article/details/41843511 上一篇文章<iOS 8界面自动布局系列-1>简要介绍了iOS界面布局方式的前世今生 ...
- 【转 iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
原文网址:http://blog.csdn.net/pucker/article/details/41843511 上一篇文章<iOS 8界面自动布局系列-1>简要介绍了iOS界面布局方式 ...
- 【转】iOS6中的Auto Layout:通过代码添加约束
最近做的项目用到了Auto Layout,于是经过了一番大量的google,这是我看到的讲用代码创建约束最清晰的一篇教程,于是想跟更多的人分享一下.原文也比较简单,可以直接过去看,如果我翻译的 ...
随机推荐
- angular-输入验证
$dirty 表单有填写记录 $valid 字段内容合法的 $invalid 字段内容是非法的 $pristine 表单没有填写记录 客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必 ...
- ioctl.h 分析
ioctl.h 分析 我自己画了个解析图...不要嫌弃丑啊.. . 哈哈 type The magic number. Just choose one number (after consulting ...
- HBase基本数据操作具体解释
引言 本文档參考最新(截止2014年7月16日)的官方Ref Guide.Developer API编写. 全部代码均基于"hbase 0.96.2-hadoop2"版本号编写.均 ...
- Linux就该这么学 20181007第十章Apache)
参考链接https://www.linuxprobe.com/ /etc/httpd/conf/httpd.conf 主配置文件 SElinux域 ---服务功能的限制 SElinux安全上下文 -- ...
- ES6 | class类的基本语法总结
类和模块的内部,默认就是严格模式,所以不需要使用use strict指定运行模式.只要你的代码写在类或模块之中,就只有严格模式可用. 考虑到未来所有的代码,其实都是运行在模块之中,所以 ES6 实际上 ...
- Codeforces 930A. Peculiar apple-tree (dfs)
题目: 代码: #include <bits\stdc++.h> using namespace std; ]; //b[i]表示距离1号花絮i步的花絮的个数 map <int, l ...
- C++调用约定和名字约定 thiscall
调用约定: __cdecl __fastcall与 __stdcall,三者都是调用约定(Calling convention),它决定以下内容:1)函数参数的压栈顺序,2)由调用者还是被调用者把参数 ...
- double int 类型的区别
内部组织格式不同: po [NSString stringWithFormat:@"%d", f] 107886912 (lldb) po [NSString stringWith ...
- sass的用法小结(三)
5. 混合器; 如果你的整个网站中有几处小小的样式类似(例如一致的颜色和字体),那么使用变量来统一处理这种情况是非常不错的选择.但是当你的样式变得越来越复杂,你需要大段大段的重用样式的代码,独立的变量 ...
- [国家集训队]最长双回文串 (PAM)回文自动机
Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #include <cstr ...