简体   繁体   中英

UIButton corner radius on high resolution screens

I'm trying to create a circular UIButton in Swift. The button is 112px width and height and the code below works well in devices with at most, 4-inches screen.

button.layer.masksToBounds = false;
button.layer.cornerRadius = button.bounds.size.height/2;
button.clipsToBounds = true;

With bigger screens the button turns like this:

意外的拐角半径

Any idea?

Thanks in advance.

You need to adjust the cornerRadius after Auto Layout has sized your button. Do this in an override of viewDidLayoutSubviews :

override func viewDidLayoutSubviews() {
    button.layer.cornerRadius = button.bounds.size.height/2
}

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