我在 viewDidLoad 中使用以下代码创建了一个 UIView (其中“secondview”显然是 UIView 的名称):
secondview = [[UIView alloc] initWithFrame:self.view.frame];
[secondview setBackgroundColor: [UIColor yellowColor]];
secondview.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:secondview];
然后在 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];
但是,约束不适用于视图(至少我看不到)。相反,视图似乎从屏幕上消失了。然而,如果约束代码被注释掉,视图将再次加载适当的框架(显然没有应用约束)。当将相同的约束应用于 Button 或 ImageView 时,约束将得到完美应用。这让我认为问题是由于创建视图时的“initWithFrame”造成的,因为按钮和 ImageView 实际上都不需要指定它的大小。
你怎么看?我应该采取什么不同的做法?
对于遇到此问题的任何人...我需要添加多个约束。这就成功了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)