简体   繁体   English

UIbutton iOS的圆角-Swift

[英]Round corners of UIbutton iOS - Swift

I am creating an application with a row of buttons. 我正在创建带有一行按钮的应用程序。 I want to set the round corners for first button and the last button. 我想为第一个按钮和最后一个按钮设置圆角。 First button with left corner and last button with right corner. 第一个按钮位于左上角,最后一个按钮位于右上角。 I am using the following code. 我正在使用以下代码。

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

My output is like the following screenshot 我的输出类似于以下屏幕截图

在此处输入图片说明

Note: Abobe screenshot from iphone6 plus Note: iphone6 plus Abobe屏幕截图

My ipad2 screenshot like this 我的ipad2屏幕截图是这样的

在此处输入图片说明

Please someone help me to find the issue. 请有人帮我找到问题。

EDIT: 编辑:

My required output need to be like this 我所需的输出必须像这样

在此处输入图片说明

Please leave the color and cross symbols. 请保留颜色和十字符号。

You shouldn't use absolute values - use relatives for rounding corners or change your radius accordingly to 您不应该使用绝对值-使用相对值来圆角或将半径相应地更改为

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

Hope, you understood the direction. 希望您理解了方向。 Your 20px are not the same on iphone6plus with Retina 3x and iPad 2 2x. 在配备Retina 3x和iPad 2 2x的iphone6plus上,您的20px不同。

I found the solution. 我找到了解决方案。 Actually the solution is pretty simple. 实际上,解决方案非常简单。 I just move the code inside of viewDidAppear() everything works fine now. 我只是将代码移到viewDidAppear()内部,现在一切正常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM