尺寸增加后圆形按钮变为圆角矩形 [英] Circular button becomes rounded rectangle after size increase
问题描述
我在iOS应用中创建了一个圆形按钮,更改了自动布局的宽度和高度限制(使按钮变大)后,它现在是一个圆角矩形而不是一个圆形.
我在Main.storyboard中定义了一个UIButton,在ViewController.swift中将其定义为IBOutlet ...
@IBOutlet weak var button: UIButton!
在viewDidLoad()
中,我在运行时更改.cornerRadius以使其为圆...
button.layer.cornerRadius = button.frame.size.width / 2.0
在我更改应用程序UIButton的高度/宽度之前,此方法对我的应用程序完全有效.我为为什么按钮不再是圆圈而感到困惑.有什么想法吗?
在viewDidLoad
之后出现自动布局,因此您需要将调整移动到viewDidLayoutSubviews
,以便将帧大小设置为正确的大小. /p>
I created a circular button in my iOS app and after changing the auto layout width and height constraints (so that the button is bigger) it is now a rounded rectangle instead of a circle.
I have a UIButton defined in Main.storyboard and as IBOutlet in ViewController.swift ...
@IBOutlet weak var button: UIButton!
In viewDidLoad()
I am changing .cornerRadius at runtime to make it a circle ...
button.layer.cornerRadius = button.frame.size.width / 2.0
This approach worked perfectly for my app until I changed the height / width of the UIButton. I am stumped as to why the button is no longer a circle. Any ideas?
Auto Layout happens after viewDidLoad
, so you need to move your adjustment to viewDidLayoutSubviews
so that the frame size will be set to the correct size.
这篇关于尺寸增加后圆形按钮变为圆角矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!