1

I have button in my ViewController in storyboard. And I add some constraints to my button in storyboard. I want to change button size in code. But my code doesn’t work. How to fix it?

@IBOutlet var font: UILabel! override func viewDidLoad() { super.viewDidLoad() font = UILabel(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(50), height: CGFloat(20))) } 
1
  • where is the button outlet and you other code for button?? Commented Jul 15, 2017 at 6:03

3 Answers 3

2

If you connect you button from code with Interface Builder and set up constraints, you can change the size of button by changing constraints' constant.

class ViewController: UIViewController { @IBOutlet var button: UIButton! @IBOutlet var heightConstaint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() } func foo() { heightConstaint.constant = 50.0 view.setNeedsLayout() } } 

If you don't use constraints, you can just change frame of view.

button.frame = CGRect(x: 0, y: 0, width: 50, height: 100)

Sign up to request clarification or add additional context in comments.

Comments

0

You can update the button constraints, update from the code, or from the storyboard to take out the constraints, modify it

Comments

0

First you create button constraints Outlet . then modify the constraints like this:

 self.buttonWidthConstaint.constant = 100.0 

Comments