简体   繁体   中英

UIButtons are not correctly shaped on all devices

I've an UIView Controller with four square buttons and I've set them round with a border. It works perfectly on iPhone 8 and iPhone X but in iPhone SE and iPhone 8 Plus the UIButtons are not round anymore. I've set the UIButtons to be square and to keep that ratio with Auto-Layout but it doesn't appear to work.

In my ViewController.Swift , I've linked the four UIButtons and then I've applied the same code as below :

@IBOutlet weak var topLeftButtonImage: UIButton!


// Edit it to round
topLeftButtonImage.layer.cornerRadius = topLeftButtonImage.frame.size.width/2
topLeftButtonImage.clipsToBounds = true

// Add border
topLeftButtonImage.layer.borderColor = UIColor.white.cgColor // Button border color
topLeftButtonImage.layer.borderWidth = 4 // Button border width

Here you can see the behaviour on iPhone SE and iPhone 8 Plus. iPhone 8 and iPhone X are fine .

iPhone模拟器截图

Auto-Layout constraints :

自动布局在行动

set button cornerRadius in viewDidLayoutSubviews method of viewcontroller

 override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    // Edit it to round
    topLeftButtonImage.layer.cornerRadius = topLeftButtonImage.bounds.size.height / 2
    topLeftButtonImage.clipsToBounds = true

    // Add border
    topLeftButtonImage.layer.borderColor = UIColor.white.cgColor // Button border color
    topLeftButtonImage.layer.borderWidth = 4 // Button border width
}

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