gpt4 book ai didi

ios - 更改 NSLayoutConstraint 常量在layoutSubviews中不起作用

转载 作者:行者123 更新时间:2023-12-03 20:40:13 25 4
gpt4 key购买 nike

当 View 动画为横向模式时,我试图更改 UIButton 的宽度。但是该方法被调用是因为我在那里设置了断点,但按钮的宽度没有改变。我将 IBOutlet 约束添加到按钮的宽度,名为: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”中的约束常量。

 -(void)viewDidLayoutSubviews
{

[super viewDidLayoutSubviews];

// Update constraint constant

[self.view layoutSubviews];

}

这对我有用。

编辑:确保没有其他约束与此宽度约束相冲突。

关于ios - 更改 NSLayoutConstraint 常量在layoutSubviews中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27288507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com