@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
button.layer.borderWidth = 0.5
button.layer.cornerRadius = 10
button.layer.masksToBounds = true
button.layoutMargins.left = 5
//roundCorners(corners: [.bottomLeft, .bottomRight], radius: button.cornerRadius)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
我有一个cornerRadius=10
的按钮。但我需要以方形显示按钮的顶部边缘。我怎么能实现这个目标呢?
答案 0 :(得分:0)
您可以使用UIBezierPath
来实现此目的let path = UIBezierPath(button.bounds,
byRoundingCorners:[.bottomRight, .bottomLeft],
cornerRadii: CGSize(width: 10, height: 10))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
button.layer.mask = maskLayer
答案 1 :(得分:0)
您可以创建UIView的扩展名,如:
public extension UIView {
/// Set some or all corners radiuses of view.
///
/// - Parameters:
/// - corners: array of corners to change (example: [.bottomLeft, .topRight]).
/// - radius: radius for selected corners.
public func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let maskPath = UIBezierPath(roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let shape = CAShapeLayer()
shape.path = maskPath.cgPath
layer.mask = shape
}
}
然后使用它来围绕你的按钮:
yourBtn.round([.bottomLeft, .bottomRight], radius: 10)
答案 2 :(得分:0)
在swift 3.0中,您可以在视图控制器的viewDidload方法中尝试此代码
let rectShape = CAShapeLayer()
rectShape.bounds = self.yourBtnName.frame
rectShape.position = self.yourBtnName.center
rectShape.path = UIBezierPath(roundedRect: self.yourBtnName.bounds, byRoundingCorners: [.bottomLeft , .bottomRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
//Here I'm masking the textView's layer with rectShape layer
self.yourBtnName.layer.mask = rectShape
你的按钮看起来像
答案 3 :(得分:0)
button.layer.cornerRadius = 2;
button.layer.borderWidth = 1;
button.layer.borderColor = UIColor.whiteColor().CGColor
并设置按钮高度和宽度(10,10)