简体   繁体   中英

Draw rounded rect border with no bottom border

I can draw a rounded corner at top left and right with that code

extension UIView {
    func roundCorners(corners: UIRectCorner, radius: CGFloat) {
      let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
      let mask = CAShapeLayer()
      mask.path = path.cgPath
      layer.mask = mask
    }
}

but what i want is a rounded rect at top left and top right with no bottom border, can anyone help me with this?

Why draw a bezierPath and apply mask when you can directly do this to your view?

yourView.layer.masksToBounds = true
yourView.layer.cornerRadius = 20 //your radius
yourView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]

This will round the top-left and top-right corners of your view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM