编辑UIButton在一边有圆angular

我对Swift相当陌生,我试图理解如何编辑UIButtons使它们看起来有某种特定的方式。 我想知道如何让我的UIButtons如下所示:

这个

尝试这个:

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 

你可以将它应用到任何UI元素,inheritanceUIView (例如UIButton)

在这种情况下,我会用白色的文字做一个透明的button。

然后我会定义边框的颜色和大小:

 self.myButton.layer.layer.borderColor = UIColor.white self.myButton.layer.layer.borderWidth = 3 

接下来,我会围绕button:

 self.myButton.layer.cornerRadius = 3 self.myButton.clipsToBounds = true 

只需将button置于此颜色视图的顶部即可