斯威夫特:如何在故事板上的按钮的一个角落绕过?

发布于 2025-02-11 21:53:18 字数 249 浏览 0 评论 0原文

我知道如何用以下方式将Uibutton的双方绕过:Mybutton.layer.cornerradius = 10,但我不确定如何只绕过一侧,以便看起来像这样:

I know how to round both sides of a UIButton with: myButton.layer.cornerRadius = 10, but I am not sure how to round just one side so it would look something like this:
Red button is rounded on left side not on right

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾听心声的旋律 2025-02-18 21:53:18

我认为您无法以IB进行操作,通常的方法是通过用户定义的运行时属性设置属性或手动设置属性。

但是,设置A角半径,但仅在某些角度需要设置具有值的图层蒙版阵列([.layerminxminyCorner,.layerminxmaxycorner]))),IB中不支持。

另一个选项是编写一个Uibutton的子类,该子类在INIT方法中设置这些值(尤其是INIT(编码器:)),然后在IB中更改以将该类用于视图。

I don't think you can do it in IB, the usual way to do it would be to either set properties or manually via the User Defined Runtime Attributes.

However, setting the a corner radius but only for certain corners requires setting the layer maskedCorners array with values ([.layerMinXMinYCorner, .layerMinXMaxYCorner]) and that is not supported in IB.

The other option would be to write a subclass of UIButton that sets those values in the init method (particularly the init(coder:)) and then change in IB to use that class for the view.

缘字诀 2025-02-18 21:53:18

将此扩展名用于您的项目

 extension UIButton {
        func roundCorners(corners: UIRectCorner, radius: Int = 8) {
            let maskPath1 = UIBezierPath(roundedRect: bounds,
                                         byRoundingCorners: corners,
                                         cornerRadii: CGSize(width: radius, height: radius))
            let maskLayer1 = CAShapeLayer()
            maskLayer1.frame = bounds
            maskLayer1.path = maskPath1.cgPath
            layer.mask = maskLayer1
        }
    }

以调用Roundcorners方法

myButton.roundCorners(corners: [.topLeft, .bottomLeft])

参考

如果您需要为所有视图组件使用扩展
更改扩展名Uibutton to 扩展名Uiview

Use this extension into your project

 extension UIButton {
        func roundCorners(corners: UIRectCorner, radius: Int = 8) {
            let maskPath1 = UIBezierPath(roundedRect: bounds,
                                         byRoundingCorners: corners,
                                         cornerRadii: CGSize(width: radius, height: radius))
            let maskLayer1 = CAShapeLayer()
            maskLayer1.frame = bounds
            maskLayer1.path = maskPath1.cgPath
            layer.mask = maskLayer1
        }
    }

to call roundCorners Method

myButton.roundCorners(corners: [.topLeft, .bottomLeft])

Reference

If you need to use extension for all view components
Change extension UIButton to extension UIView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文