更改NSLayoutConstraint常量在layoutSubviews中不起作用 [英] Change NSLayoutConstraint constant not working in layoutSubviews
本文介绍了更改NSLayoutConstraint常量在layoutSubviews中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当视图动画化为横向模式时,我正在尝试更改UIButton的宽度.但是之所以调用该方法,是因为我在此处设置了一个断点,但是按钮的宽度没有改变.我将IBOutlet约束添加到名为widthglobalButtonWidthConstraint的按钮宽度上.
I am trying to change a UIButton's width when the view animates to landscape mode. But the method is called because I set a break point there, but the button's width doesn't change. I add a IBOutlet constraint to button's width named: globalButtonWidthConstraint.
我当前的代码:
- (void)layoutSubviews {
[super layoutSubviews];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat screenH = screenSize.height;
CGFloat screenW = screenSize.width;
BOOL isLandscape = !(self.frame.size.width == (screenW*(screenW<screenH))+(screenH*(screenW>screenH)));
if (isLandscape) {
self.globalButtonWidthConstraint.constant = 100;
[self layoutIfNeeded];
} else {
self.globalButtonWidthConstraint.constant = 47;
[self layoutIfNeeded];
}
}
推荐答案
尝试在"viewDidLayoutSubviews"中更新约束常量.
Try updating constraint constant in "viewDidLayoutSubviews".
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Update constraint constant
[self.view layoutSubviews];
}
对我有用.
编辑:确保没有其他与该宽度约束冲突的约束.
Make sure that there is no other constraints that conflicting this width constraint.
这篇关于更改NSLayoutConstraint常量在layoutSubviews中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文