[英]How do I update constraints after changing UIButton's image
I have a mini game in my project where you have to guess a flag of a country, I don't use Interface Builder, all UI is written in code.我的项目中有一个小游戏,你必须猜一个国家的国旗,我不使用界面生成器,所有的 UI 都是用代码编写的。 When user taps one of the flags I load new set of countries to guess, set their flags to buttons and since flags are different in sizes from previous ones I need to adjust UIButton constraints so I call my function setupConstraints() which looks like that:当用户点击其中一个标志时,我会加载一组新的国家/地区进行猜测,将它们的标志设置为按钮,并且由于标志的大小与以前的不同,我需要调整 UIButton 约束,因此我调用了我的函数 setupConstraints() ,它看起来像这样:
func setupConstraints() {
NSLayoutConstraint.activate([
countryNameLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
countryNameLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
countryNameLabel.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -20),
countryNameLabel.heightAnchor.constraint(equalToConstant: 30),
firstCountryButton.topAnchor.constraint(equalTo: countryNameLabel.bottomAnchor, constant: 35),
firstCountryButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
firstCountryButton.heightAnchor.constraint(equalToConstant: 120),
firstCountryButton.widthAnchor.constraint(equalTo: firstCountryButton.heightAnchor, multiplier: flagImages[0].getAspectRatio()),
secondCountryButton.topAnchor.constraint(equalTo: firstCountryButton.bottomAnchor, constant: 25),
secondCountryButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
secondCountryButton.heightAnchor.constraint(equalToConstant: 120),
secondCountryButton.widthAnchor.constraint(equalTo: secondCountryButton.heightAnchor, multiplier: flagImages[1].getAspectRatio()),
thirdCountryButton.topAnchor.constraint(equalTo: secondCountryButton.bottomAnchor, constant: 25),
thirdCountryButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
thirdCountryButton.heightAnchor.constraint(equalToConstant: 120),
thirdCountryButton.widthAnchor.constraint(equalTo: thirdCountryButton.heightAnchor, multiplier: flagImages[2].getAspectRatio()),
])
}
This function works fine when I initially setup my View Controller but when I call it to update constraints I get in log following:当我最初设置我的视图控制器时,这个函数工作正常,但是当我调用它来更新约束时,我得到以下日志:
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x280d92940 UIButton:0x102315180.height == 120 (active)>",
"<NSLayoutConstraint:0x280d92990 UIButton:0x102315180.width == 1.5*UIButton:0x102315180.height (active)>",
"<NSLayoutConstraint:0x280db2b70 UIButton:0x102315180.width == 2*UIButton:0x102315180.height (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x280db2b70 UIButton:0x102315180.width == 2*UIButton:0x102315180.height (active)>
I see that it's trying to set width twice but I don't know why.我看到它试图设置宽度两次,但我不知道为什么。 I'm not even sure if my approach to call this function every time to setup constraints is the right one.我什至不确定我每次调用这个函数来设置约束的方法是否正确。
Here's one way to handle it:这是处理它的一种方法:
Create a struct ButtonSizeConstraints:创建一个结构 ButtonSizeConstraints:
struct ButtonSizeConstraints {
let widthConStraint: NSLayoutConstraint
let heightConstraint: NSLayoutConstraint
}
Give your view an array of ButtonSizeConstraints
, once for each button.为您的视图提供一个ButtonSizeConstraints
数组,每个按钮一次。 (You could also have separate variables firstButtonSizeConstraints
, secondButtonSizeConstraints
, etc, but by making it an array you can loop through it.) (您也可以有单独的变量firstButtonSizeConstraints
、 secondButtonSizeConstraints
等,但通过将其secondButtonSizeConstraints
数组,您可以遍历它。)
In your setupConstraints
function, put those constraints into local vars, add them to your array of ButtonSizeConstraints
, and activate them outside of the code you posted.在您的setupConstraints
函数中,将这些约束放入本地变量中,将它们添加到您的ButtonSizeConstraints
数组中,并在您发布的代码之外激活它们。
Then, when you load a new image into one or more of your buttons, fetch that button's ButtonSizeConstraints
, update that constraint's constant value, and call the parent view's layoutIfNeeded()
method.然后,当您将新图像加载到一个或多个按钮中时,获取该按钮的ButtonSizeConstraints
,更新该约束的常量值,并调用父视图的layoutIfNeeded()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.