简体   繁体   中英

Button at smaller size not round with layer.cornerRadius

I tried to create a button and style it with that code:


    button.tintColor = UIColor.white
    button.contentEdgeInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    button.backgroundColor = .clear
    button.layer.cornerRadius = 50
    button.layer.borderWidth = 2
    button.layer.borderColor = UIColor.white.cgColor

My button size was 100x100 and if I make it smaller (eg 80x80 as shown below) the border is not round anymore: 居中的“圆”为80x80,其他两个为100x100

How do I get the centered circle round aswell?

use this below code

button.layer.cornerRadius = button.frame.size.width/2

Make sure height and width are equal at every time.

Kindly set corner radius as buttonWidth/2. I assume your button is having equal width and height.

button.layer.cornerRadius = button.frame.size.width/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