我的整个程序都有一个视图控制器,我的程序由移动图像组成。问题在于,当一个人点击iAd横幅时,它似乎称为自动布局,因此自动布局的作用是自动布置我的图像,就像我在故事板中设置它们一样,如何避免这种情况发生?现在我明白了,如果我取消选中自动布局,问题将得到解决,但问题是我想在检测到 4 英寸屏幕时坚持使用自动布局来调整大小
您正在通过设置图像frame
属性来移动图像。不要在自动布局下执行此操作;一旦发生另一个布局传递,框架将重置为约束值。
您需要通过编辑约束来更改子视图的位置,方法是更改 constant
属性或删除和替换整个约束。
我自己也有同样的问题,在避开它多年以支持iOS 5支持之后,我第一次涉足自动布局。此页面帮助我解决了问题:http://matthewmorey.com/creating-uiviews-programmatically-with-auto-layout/
在viewDidLoad中,我最终称之为:
for( NSLayoutConstraint* constraint in self.view.constraints )
{
[self.view removeConstraint: constraint];
}
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewToCenter
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:center.x]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.viewToCenter
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:center.y]];
基本上,它删除了任何以前设置的约束(因为我定期使用新的居中约束更新它们),然后添加新约束以将其居中到正确的位置。所以我有这个self.viewToCenter,它从上面代码设置的self.view的左上角偏移。