screenMonkey MonkeyMan
screenMonkey MonkeyMan

Reputation: 423

How do I override Storyboard UIView height property programatically

I have placed a small UIView in my viewController and set its constraints left, right and bottom. In order not to get a constraint error I have to add a height as well. I don't want to keep this height though, as I would like it to be dynamically set in its viewController like this:

bottomBar.frame.size.height = self.view.frame.height / 10

I thought this would override the constraint value I set in the storyBoard but it doesn't work. I have set it to / 2 just to see what happens, but it still keeps the storyboard value. How do I override this?

Upvotes: 1

Views: 392

Answers (3)

Zeeshan Tufail
Zeeshan Tufail

Reputation: 506

You can create an IBOutlet connection from that constraint you added in your storyboard to the ViewController. ForExample in below image: Showing how to add IBOutlet for constraint

Once you have a connection you could do like:

self.newlyAddedConstraint.constant = self.view.frame.height / 10;

Thats all. your height will be adjusted. Depending on where you are setting this constant you might also need to call

[self.view layoutIfNeeded]

Upvotes: 1

kirander
kirander

Reputation: 2256

You can remove storyboard constraint at build time. Just choose needed constraint and select "Remove at build time".

Upvotes: 0

Yonat
Yonat

Reputation: 4608

You can link you storyboard height constraint to an outlet:

@IBOutlet var heightConstraint: NSLayoutConstraint!

then later you can change it:

heightConstraint.constant = 100

Upvotes: 1

Related Questions