使用Swift以编程方式更改UIButton的大小

时间:2022-11-11 12:25:20

I know this question has been asked before but I can't seem to make any of the solutions work. I have my buttons set up with AutoLayout, but in order to get them to fit on an iPhone 4 I need to make them shorter.

我知道之前已经问过这个问题,但我似乎无法使任何解决方案有效。我的按钮设置了AutoLayout,但是为了让它们适合iPhone 4,我需要缩短它们。

I have this, which I've tried in viewDidLoad, viewDidAppear, and viewDidLayoutSubviews (I don't really care where it goes so long as it works)

我有这个,我已经尝试过viewDidLoad,viewDidAppear和viewDidLayoutSubviews(我真的不关心它在哪里工作)

for button in choiceButtons! {
    if smallScreen {
        button.frame = CGRect(x: button.frame.origin.x, y: button.frame.origin.y, width: button.frame.width, height: 15)
        println("small")
    } else {
        button.frame = CGRect(x: button.frame.origin.x, y: button.frame.origin.y, width: button.frame.width, height: 55)
        button.frame.size.height = 55 //second attempt (what I'd really like to work)
        print("big")
    }
}

The print statements are executing correctly, so I know that works. I've checked the "Placeholder Remove at Build Time" option for all four buttons. I've tried removing the height constraint from the buttons in AutoLayout entirely (it's irrelevant, I'm just trying to keep XCode from yelling at me). Creating all four buttons completely programmatically seems tedious but doable if there's no other option, but I feel like what I'm trying to do shouldn't be too difficult. For what it's worth, if I apply the exact same code to an image, it works. Just not my button.

print语句正确执行,所以我知道它有效。我已经检查了所有四个按钮的“占位符在构建时删除”选项。我已经尝试完全从AutoLayout中的按钮中删除高度约束(这是无关紧要的,我只是想让XCode不要对我大吼大叫)。完全以编程方式创建所有四个按钮似乎很乏味,但如果没有其他选择可行,但我觉得我想要做的事情应该不会太难。对于它的价值,如果我将完全相同的代码应用于图像,它就可以工作。不是我的按钮。

How can I change the height of a button with Swift?

如何用Swift更改按钮的高度?

Not able to change the frame of UIButton Programmatically.?

无法以编程方式更改UIButton的框架。

3 个解决方案

#1


4  

In Interface Builder, create real constraints (not placeholder) for the height of each button and create an outlet collection for the constraints.

在Interface Builder中,为每个按钮的高度创建实际约束(而不是占位符),并为约束创建出口集合。

for heightConstraint in heightConstraintsForChoiceButtons! {
    if smallScreen {
        heightConstraint.constant = 15
        println("small")
    } else {
        heightConstraint.constant = 55
        print("big")
    }
}

#2


2  

There is a much better way to do this, one that will prevent the confusion you're having with constraints. Make this a button subclass and override intrinsicContentSize. Now the button will size itself to the correct size, and you can leave all your other constraints in place.

有一种更好的方法可以做到这一点,它可以防止你对约束产生的混淆。将其设为按钮子类并覆盖intrinsicContentSize。现在按钮将自己调整到正确的大小,您可以将所有其他约束保留在原位。

#3


1  

You need to manipulate the constraint constant itself. Link your height constraint to your code, and do something like this:
heightConstraint.constant = x
Put this code in viewDidLayoutSubviews

您需要自己操纵约束常量。将您的高度约束链接到您的代码,并执行以下操作:heightConstraint.constant = x将此代码放在viewDidLayoutSubviews中

#1


4  

In Interface Builder, create real constraints (not placeholder) for the height of each button and create an outlet collection for the constraints.

在Interface Builder中,为每个按钮的高度创建实际约束(而不是占位符),并为约束创建出口集合。

for heightConstraint in heightConstraintsForChoiceButtons! {
    if smallScreen {
        heightConstraint.constant = 15
        println("small")
    } else {
        heightConstraint.constant = 55
        print("big")
    }
}

#2


2  

There is a much better way to do this, one that will prevent the confusion you're having with constraints. Make this a button subclass and override intrinsicContentSize. Now the button will size itself to the correct size, and you can leave all your other constraints in place.

有一种更好的方法可以做到这一点,它可以防止你对约束产生的混淆。将其设为按钮子类并覆盖intrinsicContentSize。现在按钮将自己调整到正确的大小,您可以将所有其他约束保留在原位。

#3


1  

You need to manipulate the constraint constant itself. Link your height constraint to your code, and do something like this:
heightConstraint.constant = x
Put this code in viewDidLayoutSubviews

您需要自己操纵约束常量。将您的高度约束链接到您的代码,并执行以下操作:heightConstraint.constant = x将此代码放在viewDidLayoutSubviews中