UIIOS按钮的圆角-Swift
我正在创建一个带有一行按钮的应用程序。我想设置第一个按钮和最后一个按钮的圆角。第一个按钮带左角,最后一个按钮带右角。我正在使用以下代码UIIOS按钮的圆角-Swift,ios,swift,uibutton,Ios,Swift,Uibutton,我正在创建一个带有一行按钮的应用程序。我想设置第一个按钮和最后一个按钮的圆角。第一个按钮带左角,最后一个按钮带右角。我正在使用以下代码 //Following code from viewDidLoad function test_1.roundCorners([.TopLeft, .BottomLeft], radius: 20); test_10.roundCorners([.TopRight, .BottomRight], radius: 20); ext
//Following code from viewDidLoad function
test_1.roundCorners([.TopLeft, .BottomLeft], radius: 20);
test_10.roundCorners([.TopRight, .BottomRight], radius: 20);
extension UIView{
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.CGPath
self.layer.mask = mask
}
}
我的输出类似于下面的屏幕截图
注意:
Abobe来自iphone6plus的屏幕截图
我的ipad2屏幕截图如下
请有人帮我找到问题
编辑:
我需要的输出应该是这样的
请留下颜色和十字符号。您不应该使用绝对值-使用相对值进行圆角或根据需要更改半径
UIScreen.mainScreen().scale
UIScreen.mainScreen().nativeScale
希望,你明白方向。您的20px与配备Retina 3x和iPad2 2x的iphone6plus不同 我找到了解决办法。实际上,解决办法很简单。我只需将代码移到viewdide()中即可。
现在一切正常
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated);
test_1.roundCorners([.TopLeft, .BottomLeft], radius: 20);
test_10.roundCorners([.TopRight, .BottomRight], radius: 20);
}
检查我的编辑。这是必需的输出。