iOS开发自动布局代码篇NSLayoutConstraint

一.介绍

NSLayoutConstraint是iOS6之后出来的类

使用AutoLayout之前需要知道以下两点:

1.必须设置translatesAutoresizingMaskIntoConstraints为NO

2.如果是viewController则AutoLayout适配写在:

- (void)updateViewConstraints NS_AVAILABLE_IOS(6_0);

如果是view则AutoLayout适配写在:

- (void)updateConstraints NS_AVAILABLE_IOS(6_0) NS_REQUIRES_SUPER;

二.创建方法

NSLayoutConstraint中有两个方法:

第一个方法:

/* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant" 
 If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
 */
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

参数说明:

第一个参数view1:要设置的视图

第二个参数attr1:是个枚举,表示view1要设置的属性

第三个参数relation:是个枚举,表示视图view1和view2的指定属性之间的关系

第四个参数view2:参照的视图,可以为空

第五个参数attr2:是个枚举,表示view2的属性

第六个参数multipler:视图view1的指定属性是参照视图view2指定属性的多少倍

第七个参数c:视图view1的指定属性需要加的浮点数

根据参数的讲解,得出计算公式如下

view1.attr1 [=,>=,<=] view2.attr2 * multipler + c;

参数详情:

1.NSLayoutAttribute 是个枚举

typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
    NSLayoutAttributeLeft = 1,
    NSLayoutAttributeRight,
    NSLayoutAttributeTop,
    NSLayoutAttributeBottom,
    NSLayoutAttributeLeading,
    NSLayoutAttributeTrailing,
    NSLayoutAttributeWidth,
    NSLayoutAttributeHeight,
    NSLayoutAttributeCenterX,
    NSLayoutAttributeCenterY,
    NSLayoutAttributeLastBaseline,
    NSLayoutAttributeBaseline NS_SWIFT_UNAVAILABLE("Use 'lastBaseline' instead") = NSLayoutAttributeLastBaseline,
    NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),
    
    
    NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),
    NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),
    NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),
    NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),
    NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),
    NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),
    NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
    NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
    
    NSLayoutAttributeNotAnAttribute = 0
};

NSLayoutAttributeLeft:视图的左边

NSLayoutAttributeRight:视图的右边

NSLayoutAttributeTop:视图的上边

NSLayoutAttributeBottom:视图的底边

NSLayoutAttributeLeading:在习惯有左向右看的地区,相当于NSLayoutAttributeLeft;在习惯有右向左看的地区,相当于NSLayoutAttributeRight

NSLayoutAttributeTrailing:在习惯有左向右看的地区,相当于NSLayoutAttributeRight;在习惯有右向左看的地区,相当于NSLayoutAttributeLeft

NSLayoutAttributeWidth:视图的宽度

NSLayoutAttributeHeight:视图的高度

NSLayoutAttributeCenterX:视图的中心x轴

NSLayoutAttributeCenterY:视图的中心y轴

NSLayoutAttributeLastBaseline:相当于NSLayoutAttributeBaseline

NSLayoutAttributeBaseline:文本底标线,在大多数视图中等同于NSLayoutAttributeBottom;在少数视图,如UILabel,是指字母的底部出现的位置

NSLayoutAttributeLastFirstline:文本上标线,在大多数视图中等同于NSLayoutAttributeTop;在少数视图,如UILabel,是指字母的上部出现的位置

NSLayoutAttributeNotAnAttribute:没有

2.NSLayoutRelation 是个枚举

typedef NS_ENUM(NSInteger, NSLayoutRelation) {
    NSLayoutRelationLessThanOrEqual = -1,
    NSLayoutRelationEqual = 0,
    NSLayoutRelationGreaterThanOrEqual = 1,
};

NSLayoutRelationLessThanOrEqual:表示小于等于<=

NSLayoutRelationEqual:表示等于=

NSLayoutRelationGreaterThanOrEqual:表示大于等于>=

第二个方法VFL(Visual format language):

/* Create an array of constraints using an ASCII art-like visual format string.
 */
+ (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(nullable NSDictionary<NSString *,id> *)metrics views:(NSDictionary<NSString *, id> *)views;

参数说明:

format:视图约束的格式规范

opts:是个可选类型,表示视图的属性和布局方向

metrics:是个字典,在可视格式字符串(format)中出现的字段,必须在字典的key中找到,没找到,app就会crash

views:视图字典,这里传所有你要约束的view,NSDictionaryOfVariableBindings(view1, view2)

参数详情:

VFL语言的规则:

1."H" 表示水平方向,"V" 表示垂直方向

2."|" 表示SuperView

3."-" 表示一个间距空间,这个间距如果是view和SuperView之间,比如@"|-[button]"就是20px,如果是两个同级别的view,比如@"[button]-[button2]",那就是8px

   @"V:|-100-[button]"  表示按钮距离父视图头部100px @"V:|-100-[button]-50-|"  表示按钮距离父视图头部100px 按钮距离父视图底部50px

4."[]" 表示view,"()" 表示尺寸,可以有多个条件组合,比如@"[button(>=70,<=100)]'

5."@"表示优先级,比如@"[button(==30@1000)]"

三.添加方法

1.获取当前view中所有NSLayoutAttribute

@property(nonatomic,readonly) NSArray<__kindof NSLayoutConstraint *> *constraints NS_AVAILABLE_IOS(6_0);

2.添加NSLayoutAttribute到view或者移除NSLayoutAttribute

- (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided.  Instead, set NSLayoutConstraint's active property to YES.
- (void)addConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided.  Instead use +[NSLayoutConstraint activateConstraints:].
- (void)removeConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided.  Instead set NSLayoutConstraint's active property to NO.
- (void)removeConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided.  Instead use +[NSLayoutConstraint deactivateConstraints:].

3.iOS8新加方法,激活或者停止置顶约束

/* The receiver may be activated or deactivated by manipulating this property.  Only active constraints affect the calculated layout.  Attempting to activate a constraint whose items have no common ancestor will cause an exception to be thrown.  Defaults to NO for newly created constraints. */
@property (getter=isActive) BOOL active NS_AVAILABLE(10_10, 8_0);

/* Convenience method that activates each constraint in the contained array, in the same manner as setting active=YES. This is often more efficient than activating each constraint individually. */
+ (void)activateConstraints:(NSArray<NSLayoutConstraint *> *)constraints NS_AVAILABLE(10_10, 8_0);

/* Convenience method that deactivates each constraint in the contained array, in the same manner as setting active=NO. This is often more efficient than deactivating each constraint individually. */
+ (void)deactivateConstraints:(NSArray<NSLayoutConstraint *> *)constraints NS_AVAILABLE(10_10, 8_0);

四.代码

1.设置视图view1的宽度和高度为100

[self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100.0]];

2. 添加到view上

NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0];
//旧版方法
//[self addConstraint:leadingConstraint];
//新版方法1
[NSLayoutConstraint activateConstraints:@[leadingConstraint]]; 
//新版方法2
leadingConstraint.active = YES;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值