简体   繁体   English

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

[英]Change size of UIButton programmatically with Swift

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). 我试图从AutoLayout的按钮中完全消除高度限制(这无关紧要,我只是想防止XCode对我大吼大叫)。 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. 只是不是我的按钮。

How can I change the height of a button with Swift? 如何使用Swift更改按钮的高度?

Not able to change the frame of UIButton Programmatically.? 无法以编程方式更改UIButton的框架。

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")
    }
}

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 . 将其设置为按钮的子类,并覆盖intrinsicContentSize Now the button will size itself to the correct size, and you can leave all your other constraints in place. 现在,按钮将自动将其大小调整为正确的大小,您可以将所有其他约束保留在原处。

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 将此代码放入viewDidLayoutSubviews

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM