how to set cornerRadius for only bottom-left,bottom-right and top-left corner textview?

前端 未结 12 823
名媛妹妹
名媛妹妹 2020-11-27 12:50

How to set corner radius only only bottom-left,bottom-right and top-left corner textview?

let rectShape = CAShapeLayer()
    rectShape.backgroundColor = UICo         


        
相关标签:
12条回答
  • 2020-11-27 12:55

    (swift 4/iOS 11) Just simply say for bottom:

    yourView.clipsToBounds = true 
    yourView.layer.cornerRadius = 10
    yourView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
    

    for Up:

    yourView.clipsToBounds = true 
    yourView.layer.cornerRadius = 10
    yourView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
    

    in your case:

    yourView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
    

    Hope this help :)

    0 讨论(0)
  • 2020-11-27 12:55

    issue solved right top and right bottom work now

    Tested code in iOS 9 , 10 , 11 version

     extension UIView {
            func roundCorners(_ corners:UIRectCorner,_ cormerMask:CACornerMask, radius: CGFloat) {
                if #available(iOS 11.0, *){
                    self.clipsToBounds = false
                    self.layer.cornerRadius = radius
                    self.layer.maskedCorners = cormerMask
                }else{
                    let rectShape = CAShapeLayer()
                    rectShape.bounds = self.frame
                    rectShape.position = self.center
                    rectShape.path = UIBezierPath(roundedRect: self.bounds,    byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
                    self.layer.mask = rectShape
                }
            }
            
        }
    
    0 讨论(0)
  • 2020-11-27 13:07

    A better answer for both iOS 11 and iOS 10 bottom corner would be

     if #available(iOS 11.0, *){
            view.clipsToBounds = true
            view.layer.cornerRadius = 10
            view.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
        }else{
           let rectShape = CAShapeLayer()
           rectShape.bounds = view.frame
           rectShape.position = view.center
           rectShape.path = UIBezierPath(roundedRect: view.bounds,    byRoundingCorners: [.bottomLeft , .bottomRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
          view.layer.backgroundColor = UIColor.green.cgColor
          view.layer.mask = rectShape
      }
    

    in case this didnt work on iOS 10 and below, try running the code in viewDidLayoutSubviews() of your viewcontroller class like this

    override func viewDidLayoutSubviews() {
        if #available(iOS 11.0, *){
        }else{
           let rectShape = CAShapeLayer()
           rectShape.bounds = view.frame
           rectShape.position = view.center
           rectShape.path = UIBezierPath(roundedRect: view.bounds,    byRoundingCorners: [.bottomLeft , .bottomRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
           view.layer.backgroundColor = UIColor.green.cgColor
           view.layer.mask = rectShape
    }
    
    0 讨论(0)
  • 2020-11-27 13:09

    Here is an extension for iOS 11+

     import Foundation
     import UIKit
    
     extension UIView {
    
       func roundCorners(_ corners: CACornerMask, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
           self.layer.maskedCorners = corners
           self.layer.cornerRadius = radius
           self.layer.borderWidth = borderWidth
           self.layer.borderColor = borderColor.cgColor
    
       }
    
     }
    

    Usage:-

    self.yourView.roundCorners([.layerMaxXMaxYCorner, .layerMaxXMinYCorner], radius: 20.0, borderColor: UIColor.green, borderWidth: 1)
    
    0 讨论(0)
  • 2020-11-27 13:09

    ez way

    for subview & POPUPs [Swift 5]

    override func layoutSublayers(of layer: CALayer) {
        searchBarPopup.clipsToBounds = true
        searchBarPopup.layer.cornerRadius = 10
        searchBarPopup.layer.maskedCorners = [ .layerMaxXMinYCorner, .layerMinXMinYCorner]
    }
    

    output:

    image

    0 讨论(0)
  • 2020-11-27 13:12

    The best solution I could come up with all of the above solution is this.

    extension UIView {
        func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
            if #available(iOS 11, *) {
                var cornerMask = CACornerMask()
                if(corners.contains(.topLeft)){
                    cornerMask.insert(.layerMinXMinYCorner)
                }
                if(corners.contains(.topRight)){
                    cornerMask.insert(.layerMaxXMinYCorner)
                }
                if(corners.contains(.bottomLeft)){
                    cornerMask.insert(.layerMinXMaxYCorner)
                }
                if(corners.contains(.bottomRight)){
                    cornerMask.insert(.layerMaxXMaxYCorner)
                }
                self.layer.cornerRadius = radius
                self.layer.maskedCorners = cornerMask
    
            } else {
                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
            }
        }
    }
    

    I believe that this way the implementation is easy while selecting the sides.

    view.roundCorners([.bottomLeft, .bottomRight, .topLeft], radius: 16)
    
    0 讨论(0)
提交回复
热议问题