[英]Swift3 round two corners of UIView
I know this question already asked so many times and their solutions also available but nothing works for me. 我知道这个问题已经问过很多次了,他们的解决方案也可用,但对我来说没什么用。 I am using Swift3 and I want to round any two sides of UIView for this purpose I found the following solution
我正在使用Swift3,我想为了这个目的绕过UIView的任何两个方面,我找到了以下解决方案
Create a rectangle with just two rounded corners in swift? 在swift中创建一个只有两个圆角的矩形?
and following is my code snippet of the above solution 以下是我上述解决方案的代码片段
extension UIView {
/**
Rounds the given set of corners to the specified radius
- parameter corners: Corners to round
- parameter radius: Radius to round to
*/
func round(corners: UIRectCorner, radius: CGFloat) -> Void {
layer.addSublayer(_round(corners: corners, radius: radius))
}
/**
Rounds the given set of corners to the specified radius with a border
- parameter corners: Corners to round
- parameter radius: Radius to round to
- parameter borderColor: The border color
- parameter borderWidth: The border width
*/
func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
let mask = _round(corners: corners, radius: radius)
addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth)
}
/**
Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border
- parameter diameter: The view's diameter
- parameter borderColor: The border color
- parameter borderWidth: The border width
*/
func fullyRound(diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
layer.masksToBounds = true
layer.cornerRadius = diameter / 2
layer.borderWidth = borderWidth
layer.borderColor = borderColor.cgColor;
}
}
private extension UIView {
func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
return mask
}
func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
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)
}
}
This is the result I got. 这是我得到的结果。 I don't know what I am doing wrong.
我不知道我做错了什么。 Can anyone please solve this?
有人可以解决这个问题吗?
Code tested and worked in Swift 3. 代码在Swift 3中测试和使用。
Use below extension
to create roundCorners
. 使用以下
extension
来创建roundCorners
。
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
}
}
Usage: in viewDidLoad
用法:在
viewDidLoad
yourViewName.roundCorners(corners: [.topRight, .bottomLeft], radius: 10)
Output: 输出:
First of all you will need to Make IBOutlet for that Textfield or Button which you need to Be Rounded Corner 首先,您需要为需要圆角的Textfield或Button制作IBOutlet
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.