Jes*_*ead 16 xcode constraints ios
我在viewDidLoad中使用以下代码创建了一个UIView(其中'secondview'显然是UIView的名称):
secondview = [[UIView alloc] initWithFrame:self.view.frame];
[secondview setBackgroundColor: [UIColor yellowColor]];
secondview.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:secondview];
Run Code Online (Sandbox Code Playgroud)
然后在viewDidAppear中我向此视图添加了约束:
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:secondview attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0f constant:-20.0f];
[self.view addConstraint:constraint];
Run Code Online (Sandbox Code Playgroud)
但是,约束不适用于视图(至少不是我能看到的).相反,视图似乎只是从屏幕上消失了.但是,如果约束代码被注释掉,则视图再次加载适当的帧(显然没有应用约束).将相同的约束应用于Button或ImageView时,约束将完美应用.这让我认为问题是因为创建View时'initWithFrame',因为按钮和ImageView实际上都不需要指定它的大小.
你的想法是什么?我该怎么办?