3

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.

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)

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.

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

Not able to change the frame of UIButton Programmatically.?

Community
  • 1
  • 1
thumbtackthief
  • 6,093
  • 10
  • 41
  • 87

3 Answers3

5

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

for heightConstraint in heightConstraintsForChoiceButtons! {
    if smallScreen {
        heightConstraint.constant = 15
        println("small")
    } else {
        heightConstraint.constant = 55
        print("big")
    }
}
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • This works! I had no idea heightConstraints could be outlets, although I guess that makes sense. This changes everything! Gracias! – thumbtackthief Sep 13 '15 at 00:49
  • I spoke too soon; it works with individual height constraints, but trying to loop through them gives me a `'NSLayoutContraint' does not have a member named generator.` – thumbtackthief Sep 13 '15 at 01:59
  • 1
    @thumbtackthief You need to use an outlet collection. `@IBOutlet var heightConstraintsForChoiceButtons: Array!` or `@IBOutlet var heightConstraintsForChoiceButtons: [NSLayoutConstraint]!` – Jeffery Thomas Sep 13 '15 at 02:52
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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
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

Eilon
  • 2,698
  • 3
  • 16
  • 32