如何根据按钮的框架计算cornerRadius属性以创建圆角.
我不喜欢为每个按钮项重新定义每个角cornerRadius.
我为UIButton创建了扩展.
extension UIButton { func setRoundedCorners(){ self.layer.cornerRadius = 10 self.layer.borderWidth = 1 } }
而且我想知道每次使用此功能时如何动态计算cornerRadius.
查找函数的主要问题是为不同的按钮大小计算.cornerRadius.以下示例将显示小的差异.
例:
转角半径为10:
转角半径为15:
是否有可能找到能够计算出适合拐角半径值的函数?
解决方法
The main idea to get flexible function that will calculate proper
corner radius for different buttons based on their frame
我认为这取决于你作为论点传递的内容:
extension UIButton { func setRoundedCorners(ratio: Double?) { if let r = ratio { self.layer.cornerRadius = self.frame.size.width*r // for specific corner ratio } else { // circle // i would put a condition,as width and height differ: if(self.frame.size.width == self.frame.size.height) { self.layer.cornerRadius = self.frame.size.width/2 // for circles } else { // } } self.layer.borderWidth = 1 } }
let button = UIButton() button.setRoundedCorners(ratio: 0.25) // shape with rounded corners button.setRoundedCorners() // circle
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。