theDC
theDC

Reputation: 6484

Swift - setting UIView's size based on size screen

I went through many threads here and tried two most recommended solutions. Inside ViewDidLoad() method:

self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height * 0.254)

or

self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 0.254)

Also, in my storyboard I set low priority of the view's height constraint (If I don't set height in storyboard, xcode would complain about ambiguous layout)

But none of these lines of code above does anything to darkBackgroundWithButtons, it remains the same height for each device size

Upvotes: 0

Views: 3410

Answers (1)

Eric
Eric

Reputation: 1213

This probably is the problem:

In interface builder you set constrains to your button, and therefore it doesn't change its height when you try to update the frame. Do this instead:

First connect your constrain from interface builder to your viewcontroller, just how you would normally do it with a button.

Then use this code to change the constrain:

 var index = self.darkBackgroundWihButtons.constraints.indexOf("your constrain name")!
 self.darkBackgroundWithButtons.constraints[index].constant = 0.2 // or whatever number you want

Upvotes: 1

Related Questions