[英]UIButton corner radius on high resolution screens
I'm trying to create a circular UIButton in Swift.我正在尝试在 Swift 中创建一个圆形的 UIButton。 The button is 112px width and height and the code below works well in devices with at most, 4-inches screen.
该按钮的宽度和高度为 112 像素,下面的代码适用于最多 4 英寸屏幕的设备。
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.在自动布局调整按钮大小后,您需要调整角
cornerRadius
。 Do this in an override of viewDidLayoutSubviews
:在
viewDidLayoutSubviews
的覆盖中执行此操作:
override func viewDidLayoutSubviews() {
button.layer.cornerRadius = button.bounds.size.height/2
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.