iOS自动布局之通过代码添加约束
2015-09-29 12:41
806 查看
有的时候,需要我们通过代码手动添加一些控件的约束,怎么做呢
假使我们已经创建好了某个空间的constant约束,比如下图中鼠标选中的这个选中他,然后拖动到右边
这样 就和.m文件关联起来
打开.m函数,检查这个函数
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
self.LW_BottomView.constant =50;
}else{
self.LW_BottomView.constant =200;
}
}
这样我们就通过判断设备的方向 来更新这个高度约束的常量属性了(后面会持续更新...)
下面再写一个简单的UIButon做为示例:
//1.先创建一个Button类实例对象btn 并对其做一些简单的设置
做完了这一步 就创建好了一个button对象Btn 设置了标题和背景颜色,并将其添加到了当前view中
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[btn
setTitle:@"LWBtn"forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColorredColor]];
[self.viewaddSubview:btn];
//2.下面要对这个btn做一些约束了
/*将使用autolayout方式来布局*/
btn.translatesAutoresizingMaskIntoConstraints =NO;
/*居中显示*/
[self.viewaddConstraint:[NSLayoutConstraintconstraintWithItem:btn
attribute:NSLayoutAttributeCenterXrelatedBy:NSLayoutRelationEqualtoItem:self.viewattribute:NSLayoutAttributeCenterXmultiplier:1constant:0]];
通过self.view addConstraint这个方法来添加约束,上面的参数的意思分别是:
constraintWithItem:第一个对象btn(view1)
attribute:第一个view的属性attr1
relatedBy:指定左右两个view之间的关系relation
toItem:指定约束右边的view2
attribute:指定第二个view2的attribute属性attr2
multiplier:指定一个与view2属性相乘的乘数
这里设置为1
constant:指定一个与view2属性相加的浮点数
这里设置为0
这个函数的对照公式为:view1.attr1
<relation> view2.attr2*multiplier+constant
注意:如果你想设置的约束里不需要view2,需要将第4个参数设置为nil,并且第5个参数设置为NSLayoutAttributeNotAnAttribute
再举个例子:
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeRight multiplier:1 constant:10]
这个约束的含义就是view1的左侧在view2右侧还要多10个点的地方
视图的属性和关系的值:
typedefNS_ENUM(NSInteger, NSLayoutRelation) {
NSLayoutRelationLessThanOrEqual = -1,//小于等于
NSLayoutRelationEqual =0,//等于
NSLayoutRelationGreaterThanOrEqual =1,//大于等于
};
typedefNS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft =1,//左侧
NSLayoutAttributeRight,//右侧
NSLayoutAttributeTop,//上方
NSLayoutAttributeBottom,//下方
NSLayoutAttributeLeading,//首部
NSLayoutAttributeTrailing,//尾部
NSLayoutAttributeWidth,//宽度
NSLayoutAttributeHeight,//高度
NSLayoutAttributeCenterX,//X轴中心
NSLayoutAttributeCenterY,//Y轴中心
NSLayoutAttributeBaseline,//文本底标线
NSLayoutAttributeLastBaseline =NSLayoutAttributeBaseline,
NSLayoutAttributeFirstBaselineNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeLeftMarginNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeRightMarginNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTopMarginNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeBottomMarginNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeLeadingMarginNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTrailingMarginNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterXWithinMarginsNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterYWithinMarginsNS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeNotAnttribute =0//没有属性
};
下面再来个简单的例子:
- (void)viewDidLoad {
[super
viewDidLoad];
[self.view
setBackgroundColor:[UIColor
whiteColor]];
btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[btn
setTitle:@"LWBtn"
forState:UIControlStateNormal];
[btn
setBackgroundColor:[UIColor
greenColor]];
[self.view
addSubview:btn];
/*将使用autolayout方式来布局*/
btn.translatesAutoresizingMaskIntoConstraints =
NO;
/*居中显示*/
[self.view
addConstraint:[NSLayoutConstraint
constraintWithItem:btn
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]];
/*距离底部20单位*/
[self.view
addConstraint:[NSLayoutConstraint
constraintWithItem:btn
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-20]];
/*定义高度是父view的1/3*/
[self.view
addConstraint:[NSLayoutConstraint
constraintWithItem:btn
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.3
constant:0]];
NSLayoutConstraint *con = [NSLayoutConstraint
constraintWithItem:btn
attribute:NSLayoutAttributeHeight
relatedBy:
NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:0.3
constant:0];
/*设置了优先级*/
con.priority =
UILayoutPriorityDefaultHigh;
[self.view
addConstraint:con];
/*设置btn最小高度为150*/
[btn
addConstraint:[NSLayoutConstraint
constraintWithItem:btn
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:150]];
/*注册KVO方法Key-Value-Observing*/
[btn
addObserver:self
forKeyPath:@"bounds"
options:NSKeyValueObservingOptionNew
context:nil];
}
//KVO回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString
*,id> *)change context:(void *)context{
[btn
setTitle:NSStringFromCGSize(btn.bounds.size)
forState:UIControlStateNormal];
NSLog(@"改变的属性:%@",keyPath);
}
下面是几片推荐的文章,写的不错
/article/8376940.html
http://doc.okbase.net/u013263917/archive/121406.html
相关文章推荐
- IOS获取系统图片
- 解决iOS 9 symbolicatecrash卡死
- iOS 9学习系列:上手SFSafariViewController
- iOS开发实习期间get的技巧
- iOS 9适配技巧(更新版)
- iOS开发-图片加载优化
- [IOS]从零开始搭建基于Xcode7的IOS开发环境和免开发者帐号真机调试运行第一个IOS程序HelloWorld
- ios 除数为0时
- IOS欢迎界面Launch Screen动态加载广告
- iOS9中Bitcode的介绍及配置
- iOS Designated Initializers : Using NS_DESIGNATED_INITIALIZER
- iOS 使用AVFoundation 扫描二维码并限定扫描区域(带代码生成蒙版)
- note: declarations in dependent base ‘std::basic_ios<char>’ are not found by unqualified lookup
- iOS中如何使用PCH文件、修改其相对路径的方法
- [iOS开发项目-5] 图片浏览器
- iOS数据本地化
- XCODE4.6创建我的第一次ios规划:hello
- iOS_正则表达式判断手机型号、邮箱、手机号、身份证、昵称、密码等
- 正则表达式在iOS中的运用
- 修改ios系统文件使Xcode编译出错