简体   繁体   中英

How to change button size?

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

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)

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

 self.buttonWidthConstaint.constant = 100.0

您可以更新按钮约束,从代码更新或从情节提要中删除约束,对其进行修改

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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