![](/img/trans.png)
[英]Programmatically Setting Corner Radius for UIButtons in Landscape and Portrait Mode for All iPhone and iPad Devices
[英]UIButtons are not correctly shaped on all devices
我有一个带有四个方形按钮的 UIView 控制器,并将它们设置为带有边框的圆形。 它在 iPhone 8 和 iPhone X 上完美运行,但在 iPhone SE 和 iPhone 8 Plus 中,UIButtons 不再是圆形的。 我已将 UIButtons 设置为方形并通过自动布局保持该比例,但它似乎不起作用。
在我的ViewController.Swift 中,我链接了四个 UIButtons,然后我应用了如下相同的代码:
@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
在这里您可以看到 iPhone SE 和 iPhone 8 Plus 上的行为。 iPhone 8 和 iPhone X 没问题。
自动布局约束:
在viewcontroller的viewDidLayoutSubviews方法中设置按钮cornerRadius
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.