我遇到了与 this poster 类似的问题.并且我使用了 jrturton 的建议将自定义按钮的代码移动到 viewDidLayoutSubviews
中。在我收到此错误之前,它一直运行良好:
'NSInternalInconsistencyException', reason: 'Auto Layout still required after sending -viewDidLayoutSubviews to the view controller. ViewController's implementation needs to send -layoutSubviews to the view to invoke auto layout.'
我对图形一无所知,我唯一能想到的就是放置 [self.view layoutSubviews];
但这并没有解决任何问题。当我取消选中 Storyboard 中的“自动布局”时它起作用了,但这改变了我的按钮的尺寸,我想知道是否有另一种方法来修复它?
代码:
-(void)viewDidLayoutSubviews {
NSArray *arrayOfButtons = [NSArray arrayWithObjects:self.decimalButton, self.buttonOne, self.buttonTwo, self.buttonThree, nil];
for (UIButton *button in arrayOfButtons) {
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
button.layer.borderWidth = 0.25f;
button.layer.borderColor = [[UIColor grayColor] CGColor];
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = button.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:122.0f / 255.0f green:188.0f / 255.0f blue:255.0f / 255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:96.0f / 255.0f green:171.0f / 255.0f blue:248.0f / 255.0f alpha:1.0f] CGColor],
nil];
[button.layer insertSublayer:btnGradient atIndex:0];
}
}
最佳答案
我最近遇到了这个问题,当时我想通过提供 viewDidLayoutSubviews
消息处理程序以编程方式调整 Storyboard创建的 View 的框架。按照异常消息的说明,我尝试在 -viewDidLayoutSubviews
的末尾添加 [self.view layoutIfNeeded]
,然后成功了。
关于ios - 自动布局错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15490140/