UI按钮iOS的圆角 - Swift

时间:2015-11-06 09:23:48

标签: ios swift uibutton

我正在创建一个带有一行按钮的应用程序。我想为第一个按钮和最后一个按钮设置圆角。左角的第一个按钮和右角的最后一个按钮。我使用以下代码。

//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
    }
}

我的输出类似于以下屏幕截图

enter image description here

来自Note:

iphone6 plus Abobe屏幕截图

我的ipad2截图

enter image description here

请有人帮我找到问题。

编辑:

我需要的输出必须像这样

enter image description here

请留下颜色和十字符号。

2 个答案:

答案 0 :(得分:0)

你不应该使用绝对值 - 使用亲戚来圆角或相应地改变你的半径

UIScreen.mainScreen().scale
UIScreen.mainScreen().nativeScale

希望,你了解方向。你的20px在配备Retina 3x和iPad 2 2x的iphone6plus上是不一样的。

答案 1 :(得分:0)

我找到了解决方案。实际上解决方案非常简单。我只是移动viewDidAppear()内的代码现在一切正常。

override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated);
        test_1.roundCorners([.TopLeft, .BottomLeft], radius: 20);
        test_10.roundCorners([.TopRight, .BottomRight], radius: 20);
    }