Making a UIButton a % of the screen size

前端 未结 4 1454
再見小時候
再見小時候 2021-01-13 00:57

I noticed certain button sizes look great on one the iPhone 5 simulator but do not look as good on the iPhone 6 simulator and this is because the heights or constraints that

4条回答
  •  醉梦人生
    2021-01-13 01:16

    Swift 4.2

    In code it's really easy:

    override func viewDidLoad() {
            super.viewDidLoad()
    
            view.addSubview(button)
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    
            button.widthAnchor.constraint(equalToConstant: 100).isActive = true
            button.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height * 0.4).isActive = true
        }
    

    or use this instead of current heightAnchor:

    button.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.4).isActive = true
    

    hope this help :)

    
    

提交回复
热议问题