如何修改外键约束动态修改约束NSLayoutConstraint

IOS页面自动布局 之 NSLayoutConstraint基础篇
使用AutoLayout之前需要知道以下两点:
1.必须设置 translatesAutoresizingMaskIntoConstraints为NO。
2.如果是viewControl则AutoLayout适配写在[- updateViewConstraints]中;
&如果是view则AutoLayout适配写在[- updateConstraints]中。
&一、要讲解的方法:
1 /* 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.
4 +(instancetype)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier
constant:(CGFloat)c;
参数说明:
第一个参数 view1: 要设置的视图;
第二个参数 attr1: view1要设置的属性,稍后详解;
第三个参数 relation: 视图view1和view2的指定属性之间的关系,稍后详解;
第四个参数 view2: 参照的视图;
第五个参数 attr2: 参照视图view2的属性,稍后详解;
第六个参数 multiplier: 视图view1的指定属性是参照视图view2制定属性的多少倍;
第七个参数 c: 视图view1的指定属性需要加的浮点数。
根据参数的讲解,得出计算公式如下:
view1.attr1 [= , &= , &=] view2.attr2 * multiplier +
参数详解:
1、NSLayoutAttribute
1 typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = 1,
NSLayoutAttributeRight,
NSLayoutAttributeTop,
NSLayoutAttributeBottom,
NSLayoutAttributeLeading,
NSLayoutAttributeTrailing,
NSLayoutAttributeWidth,
NSLayoutAttributeHeight,
NSLayoutAttributeCenterX,
NSLayoutAttributeCenterY,
NSLayoutAttributeBaseline,
NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,
NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_(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
分三部分解释 NSLayoutAttribute
第一部分:常用的
NSLayoutAttributeLeft: CGRectGetMinX(view.frame);
NSLayoutAttributeRight: CGRectGetMaxX(view.frame);
NSLayoutAttributeTop: CGRectGetMinY(view.frame);
NSLayoutAttributeBottom: CGRectGetMinY(view.frame);
NSLayoutAttributeWidth: CGRectGetWidth(view.frame);
NSLayoutAttributeHeight: CGRectGetHeight(view.frame);
NSLayoutAttributeCenterX: view.center.x;
NSLayoutAttributeCenterY:view.center.
NSLayoutAttributeBaseline: 文本底标线,在大多数视图中等同于NSLayoutAttributeBottom; 在少数视图,如UILabel,是指字母的底部出现的位置;
NSLayoutAttributeLastBaseline: 相当于NSLayoutAttributeB
NSLayoutAttributeFirstBaseline: 文本上标线;
NSLayoutAttributeNotAnAttribute: N
第二部分: 根据国家使用习惯不同表示的意思不同
NSLayoutAttributeLeading: 在习惯由左向右看的地区,相当于NSLayoutAttributeLeft;在习惯从右至左看的地区,相当于NSLayoutAttributeR
NSLayoutAttributeTrailing: 在习惯由左向右看的地区,相当于NSLayoutAttributeRight;在习惯从右至左看的地区,相当于NSLayoutAttributeL
第三部分:ios8新增属性,各种间距,具体用法下节介绍
NSLayoutAttributeLeftMargin,
NSLayoutAttributeRightMargin,
NSLayoutAttributeTopMargin,
NSLayoutAttributeBottomMargin,
NSLayoutAttributeLeadingMargin,
NSLayoutAttributeTrailingMargin,
NSLayoutAttributeCenterXWithinMargins,
NSLayoutAttributeCenterYWithinMargins,
&从网上找了一张图,标注以上属性
2、NSLayoutRelation
1 typedef NS_ENUM(NSInteger, NSLayoutRelation) {
2 & & NSLayoutRelationLessThanOrEqual = -1,
3 & & NSLayoutRelationEqual = 0,
4 & & NSLayoutRelationGreaterThanOrEqual = 1,
&NSLayoutRelationLessThanOrEqual: &=;
&NSLayoutRelationEqual: =;
&NSLayoutRelationGreaterThanOrEqual: &=;
二、要讲解的方法
1、获取当前view中所有的 NSLayoutConstraint
1 - (NSArray *)constraints NS_AVAILABLE_IOS(6_0);
2、旧版方法,将指定的NSLayoutConstraint添加到页面或者从页面中移除
1 1 - (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.
2 2 - (void)addConstraints:(NSArray *)constraints NS_AVAILABLE_IOS(6_0); // This method will be deprecated in a future release and should be avoided.
Instead use +[NSLayoutConstraint activateConstraints:].
3 3 - (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.
4 4 - (void)removeConstraints:(NSArray *)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新加方法,激活或者停用指定约束
1 /* 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. */
2 @property (getter=isActive) BOOL active NS_AVAILABLE(10_10, 8_0);
4 /* 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 inpidually. */
5 + (void)activateConstraints:(NSArray *)constraints NS_AVAILABLE(10_10, 8_0);
7 /* 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 inpidually. */
8 + (void)deactivateConstraints:(NSArray *)constraints NS_AVAILABLE(10_10, 8_0);
三、Coding Time
a& 设置视图view1为 宽度=20的正方形
两种写法,第一种 宽度=20,高度=20
1 & & [self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]];
2 & & [self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]];
第二种 宽度=20, 高度=宽度
1 & & [self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20]];
2 & & [self addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
第二种方法的优势是,如果想修改view1的大小,只需要修改一处。
b&设置视图view1.frame.origin.x = 视图view2.frame.origin.x
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
//旧版方法
//[self addConstraint:leftConstraint];
//新版方法1
[NSLayoutConstraint activateConstraints:@[leftConstraint]];
//新版方法2
leftConstraint.active = YES;30090人阅读
Objective-C(13)
iOS开发(21)
本系列的第一篇文章介绍了自动布局的基本原理,第二篇文章通过一个简单的例子演示了如何使用Xcode的Interface Builder(简称IB)以可视化方式添加约束。本篇为该系列的第三篇文章,主要介绍如何通过写代码来添加布局约束。
说句题外话,通过IB可视化加约束,与写代码加约束,这两种方式各有优缺点。通过代码构建自动布局约束是最基础,也是最灵活的方式,但缺点是对于复杂界面相对繁冗而又容易出错。而IB通过可视化方式,把约束以直观简单的方式呈现出来,并且能够在设计器中实时预览布局效果,但是缺点是并非所有的约束都能用IB来添加,而且不容易后期维护。所以掌握写代码添加自动布局约束是非常必要的。原本这篇文章是本系列的第二篇,但是为了提高读者理解和接受的程度,最终还是把本编放在第三的位置。闲言少叙,我们进入正题。
第一篇文章中讲到,每一个布局约束就是一个明确的线性变化规则,在数学上是以一次函数的形式表示,即:
y = m * x + c   (公式3.1)
在UIKit中,每一个布局约束是一个NSLayoutConstraint实例,NSLayoutConstraint类的主要属性定义如下:
NS_CLASS_AVAILABLE_IOS(6_0)
@interface NSLayoutConstraint : NSObject
@property (readonly, assign) id firstI
@property (readonly) NSLayoutAttribute firstA
@property (readonly) NSLayoutR
@property (nullable, readonly, assign) id secondI
@property (readonly) NSLayoutAttribute secondA
@property (readonly) CGFloat
@property CGFloat
+(instancetype)constraintWithItem:(id)firstItem attribute:(NSLayoutAttribute)firstAttribute
relatedBy:(NSLayoutRelation)relation
toItem:(id)secondItem attribute:(NSLayoutAttribute)secondAttribute
multiplier:(CGFloat)multiplier constant:(CGFloat)
其中的firstItem与secondItem分别是界面中受约束的视图与被参照的视图。它们不一定非得是兄弟关系或者父子关系,只要它们有着共同的祖先视图即可,这一点可是autoresizingMask无法做到的。
firstAttribute与secondAttribute分别是firstItem与secondItem的某个布局属性(NSLayoutAttribute):
typedef NS_ENUM(NSInteger, NSLayoutAttribute)
NSLayoutAttributeLeft = 1,
NSLayoutAttributeRight,
NSLayoutAttributeTop,
NSLayoutAttributeBottom,
NSLayoutAttributeLeading,
NSLayoutAttributeTrailing,
NSLayoutAttributeWidth,
NSLayoutAttributeHeight,
NSLayoutAttributeCenterX,
NSLayoutAttributeCenterY,
NSLayoutAttributeBaseline,
NSLayoutAttributeNotAnAttribute = 0,
每一个枚举值代表了一个布局属性,名字都很直观,例如Left代表左侧,Height代表高度等等。注意,firstItem与secondItem不一定非得是同样的值,允许定义诸如某视图的高度等于另一个视图的宽度这样的约束(尽管很少这样做)。NSLayoutAttributeNotAnAttribute这个额外解释一下,当我们需要为某个视图指定固定宽度或者高度时,这时候secondItem为nil,secondAttribute为NSLayoutAttributeNotAnAttribute。
relation定义了布局关系(NSLayoutRelation):
typedef NS_ENUM(NSInteger, NSLayoutRelation)
NSLayoutRelationLessThanOrEqual = -1,
NSLayoutRelationEqual = 0,
NSLayoutRelationGreaterThanOrEqual = 1,
布局关系不仅限于相等,还可以是大于等于或者小于等于,这种不等关系在处理UILabel、UIImageView等具有自身内容尺寸的控件(自身内容尺寸参见本系列第五篇文章)时非常常用。举个简单的例子,UILabel的长度会随文字的长度而变化,那么我们可以向UILabel控件添加两个约束,分别是“长度大于等于50”与“长度小于等于200”。这样,当文字很少时,宽度也至少为50;当文字非常多时,宽度也不会超过200。
multiplier即比例系数。constant即常量。
因此,每个约束就对应如下关系:
firstItem.firstAttribute {==,&=,&=} secondItem.secondAttribute * multiplier + constant   (公式3.2)
我们可以调用NSLayoutConstraint类的constraintWithItem:…方法,传入所有需要的参数构造一个新的约束。
理论就到此为止,下面我们还是以第二篇的例子来讲解如何使用代码添加约束。
打开Xcode(8.2.1版),新建Single View Application项目,项目命名为AutoLayoutByConstraint,本文使用Objective-C讲解,设备选择Universal。下载苹果Logo图片apple.jpg,并将其拖入项目中。文件下载地址:
链接: 密码:e4ff
首先,界面上方用来显示苹果Logo图片的是一个UIImageView,ViewController类的viewDidLoad方法如下:
- (void)viewDidLoad
[super viewDidLoad];
UIImageView* logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.jpg"]];
logoImageView.contentMode = UIViewContentModeScaleAspectF
[self.view addSubview:logoImageView];
我们需要为logoImageView其添加4个约束:
- logoImageView左侧与父视图左侧对齐
- logoImageView右侧与父视图右侧对齐
- logoImageView顶部与父视图顶部对齐
- logoImageView高度为父视图高度一半
根据公式3.2,在ViewController类的viewDidLoad方法末尾处构造上述4个约束,代码如下:
NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:logoImageView.superview attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
NSLayoutConstraint* rightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:logoImageView.superview attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:logoImageView.superview attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:logoImageView.superview attribute:NSLayoutAttributeHeight multiplier:0.5f constant:0.0f];
leftConstraint.active = YES;
rightConstraint.active = YES;
topConstraint.active = YES;
heightConstraint.active = YES;
UIView类提供了若干方法和属性,用于添加或者移除约束。对于iOS 6或者iOS 7可以调用addConstraint(s):和removeConstraint(s):方法;对于iOS 8及更新的版本,直接设置约束的active属性(BOOL值)或者调用activateConstraints:与deactivateConstraints:类方法。
就是这么简单!现在编译并运行项目,
貌似logoImageView的尺寸不太对。如果在viewDidLoad方法中设置self.view的背景色为红色,看得会更清楚:
同时注意到Xcode控制台打印出了一大段信息:
2017-02-15 16:44:13.453948 AutoLayoutByConstraint[17260:1271951] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
(1) look at each constraint and try to figure out which you don't
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
"&NSAutoresizingMaskLayoutConstraint:0xfe0 h=--& v=--& UIImageView:0x7facc44020a0.width == 241
(active)&",
"&NSLayoutConstraint:0xc0 H:|-(0)-[UIImageView:0x7facc44020a0]
(active, names: '|':UIView:0x7facc4506110 )&",
"&NSLayoutConstraint:0x UIImageView:0x7facc44020a0.trailing == UIView:0x7facc4506110.trailing
(active)&",
"&NSLayoutConstraint:0xc0 'UIView-Encapsulated-Layout-Width' UIView:0x7facc4506110.width == 320
(active)&"
//后面省略若干字。。。我简要翻译一下:
不能同时满足约束。或许下列约束中的其中一个是你并不想要的。尝试如下方法:
(1) 检查每个约束,试着找出并不期望的约束。
(2) 找到添加该约束的代码,并进行修正。
(备注:如果你看到NSAutoresizingMaskLayoutConstraint却并不理解,请查阅UIview文档中的translatesAutoresizingMaskIntoConstraints属性。)
看来是出错了,为什么会这样?这是由于自动布局技术是苹果在iOS 6当中新加入的,但在那时仍然有很多项目代码使用autoresizingMask与setFrame:的方式构建界面。试想,如果将一个已经设置好frame并使用autoresizingMask的视图添加到一个使用自动布局的视图中时,运行时需要隐式地将前者的frame和autoresizingMask转化为自动布局约束(这些隐式转换的约束的类型为NSAutoresizingMaskLayoutConstraint),这样才能明确其位置与尺寸而不会导致约束的缺失。这个隐式转换的过程,是由UIView的translatesAutoresizingMaskIntoConstraints属性的值决定的。默认情况下,为了保证兼容性,该值为YES,表示需要自动进行隐式转换。这对于兼容旧的代码当然是好的,然而当我们明确为视图添加了约束后,我们就不希望再进行autoresizingMask的隐式转换了,否则就会引起约束的冲突。因此,需要特别注意的是,当我们使用代码创建视图时,需要将translatesAutoresizingMaskIntoConstraints属性的值设置为NO。在viewDidLoad方法中创建logoImageView的代码之后,添加如下代码:
logoImageView.translatesAutoresizingMaskIntoConstraints = NO;
再次运行,这次就没问题了。
到这里,我想你应该可以把剩余的视图和约束的代码添加上了,全部代码如下:
- (void)viewDidLoad
[super viewDidLoad];
UIImageView* logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.jpg"]];
logoImageView.translatesAutoresizingMaskIntoConstraints = NO;
logoImageView.contentMode = UIViewContentModeScaleAspectF
[self.view addSubview:logoImageView];
NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
NSLayoutConstraint* rightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5f constant:0.0f];
leftConstraint.active = YES;
rightConstraint.active = YES;
topConstraint.active = YES;
heightConstraint.active = YES;
UIScrollView* scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
NSLayoutConstraint* scrollLeftConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
NSLayoutConstraint* scrollRightConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
NSLayoutConstraint* scrollBottomConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
NSLayoutConstraint* scrollTopConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:logoImageView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
scrollLeftConstraint.active = YES;
scrollRightConstraint.active = YES;
scrollBottomConstraint.active = YES;
scrollTopConstraint.active = YES;
UILabel* nameLabel = [UILabel new];
nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
nameLabel.text = @"苹果公司";
nameLabel.backgroundColor = [UIColor greenColor];
[scrollView addSubview:nameLabel];
UILabel* descriptionLabel = [UILabel new];
descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
descriptionLabel.text = @"苹果公司(Apple Inc. )是美国的一家高科技公司。由史蒂夫·乔布斯、斯蒂夫·沃兹尼亚克和罗·韦恩(Ron Wayne)等三人于日创立,并命名为美国苹果电脑公司(Apple Computer Inc. ), 日更名为苹果公司,总部位于加利福尼亚州的库比蒂诺。\n苹果公司创立之初主要开发和销售的个人电脑,截至2014年致力于设计、开发和销售消费电子、计算机软件、在线服务和个人计算机。苹果的Apple II于1970年代助长了个人电脑革命,其后的Macintosh接力于1980年代持续发展。该公司硬件产品主要是Mac电脑系列、iPod媒体播放器、iPhone智能手机和iPad平板电脑;在线服务包括iCloud、iTunes Store和App Store;消费软件包括OS X和iOS操作系统、iTunes多媒体浏览器、Safari网络浏览器,还有iLife和iWork创意和生产力套件。苹果公司在高科技企业中以创新而闻名世界。\n苹果公司日公开招股上市,2012年创下6235亿美元的市值记录,截至2014年6月,苹果公司已经连续三年成为全球市值最大公司。苹果公司在2014年世界500强排行榜中排名第15名。日,在宏盟集团的“全球最佳品牌”报告中,苹果公司超过可口可乐成为世界最有价值品牌。2014年,苹果品牌超越谷歌(Google),成为世界最具价值品牌 。";
descriptionLabel.numberOfLines = 0;
descriptionLabel.backgroundColor = [UIColor yellowColor];
[scrollView addSubview:descriptionLabel];
NSLayoutConstraint* nameLabelLeftConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
NSLayoutConstraint* nameLabelRightConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
NSLayoutConstraint* nameLabelBottomConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:descriptionLabel attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
NSLayoutConstraint* nameLabelTopConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
NSLayoutConstraint* nameLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:20.0f];
nameLabelLeftConstraint.active = YES;
nameLabelRightConstraint.active = YES;
nameLabelBottomConstraint.active = YES;
nameLabelTopConstraint.active = YES;
nameLabelHeightConstraint.active = YES;
NSLayoutConstraint* descriptionLabelLeftConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
NSLayoutConstraint* descriptionLabelRightConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
NSLayoutConstraint* descriptionLabelBottomConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
descriptionLabelLeftConstraint.active = YES;
descriptionLabelRightConstraint.active = YES;
descriptionLabelBottomConstraint.active = YES;
NSLayoutConstraint* nameLabelWidthConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:logoImageView attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f];
NSLayoutConstraint* descriptionLabelWidthConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:logoImageView attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f];
nameLabelWidthConstraint.active = YES;
descriptionLabelWidthConstraint.active = YES;
程序最终项目文件链接:链接:
密码: nb3u
自动布局约束是通过描述视图间的关系而非强加坐标值来进行定位的,它更能满足不同设备尺寸的界面布局,并且更容易让人理解。虽然上面的代码很冗长,但每一句所描述的事实都十分清楚。在此省略自动布局的好处10000字。。。
区区几个简单的视图,就要写这么长的代码。。。
估计你看得有点眼花缭乱了吧,其实我也是修改并检查了好几次,又调试了好几次才完全写对的。在下一篇文章中,我将介绍另一种更简洁的方式,即使用VFL来添加约束,敬请期待吧。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:451948次
积分:2832
积分:2832
排名:第12992名
原创:28篇
评论:124条
(1)(1)(2)(1)(2)(2)(2)(1)(1)(1)(2)(1)(2)(2)(1)(6)(2)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 masonry 修改约束 的文章

 

随机推荐