编辑 UIButton 在一侧有圆角 [英] Editing UIButton to have rounded corners on one side
本文介绍了编辑 UIButton 在一侧有圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 Swift 还是比较陌生,我正在尝试了解如何编辑 UIButton 以使其看起来像某种方式.我想知道如何让我的 UIButtons 看起来像:
I'm fairly new to Swift and I'm trying to understand how to edit UIButtons to make them look a certain way. I would like to know how I can make my UIButtons look like:
推荐答案
试试这个:
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor?, borderWidth: CGFloat?) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.frame = self.bounds
mask.path = path.cgPath
self.layer.mask = mask
if borderWidth != nil {
addBorder(mask, borderWidth: borderWidth!, borderColor: borderColor!)
}
}
private func addBorder(_ mask: CAShapeLayer, borderWidth: CGFloat, borderColor: UIColor) {
let borderLayer = CAShapeLayer()
borderLayer.path = mask.path
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = borderColor.cgColor
borderLayer.lineWidth = borderWidth
borderLayer.frame = bounds
layer.addSublayer(borderLayer)
}
}
用法:
someView.roundCorners([.topLeft, .topRight], radius: 3, borderColor: nil, borderWidth: nil) //top corners with radius 3 without border
someView.roundCorners(.allCorners, radius: 2, borderColor: UIColor.red, borderWidth: 1) //all corners with red border
您可以将此应用于任何继承 UIView
(例如 UIButton)的 UI 元素
you can apply this to any UI element, that inherits UIView
(for example UIButton)
这篇关于编辑 UIButton 在一侧有圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文