skip to Main Content

Here you can see the view which is inside the TableViewell:

Here is the code for adding dashed border:

func addDashedBorder() 
{ 
    let color = UIColor(displayP3Red: 178.0/255.0, green: 181.0/255.0, blue: 200.0/255.0, alpha: 1.0).cgColor
    let shapeLayer:CAShapeLayer = CAShapeLayer()
    let shapeRect = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
    shapeLayer.bounds = shapeRect
    shapeLayer.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = color
    shapeLayer.lineWidth = 2
    shapeLayer.lineJoin = CAShapeLayerLineJoin.round
    shapeLayer.lineDashPattern = [6,3]
    shapeLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 4).cgPath
    self.layer.addSublayer(shapeLayer)
}

I’m calling this function inside the awake from nib like this:
view.addDashedBorder().

2

Answers


  1. Reason for issue: You are calling your border adding code from awakeFromNib.

    • Awake from is called when nib loaded, so that time your cell will not have the exact size for all subviews, when awakeFromNib gets called, all views have size as same as nib design. so awake from nib is the wrong place when you are setting or updating something which is related to size of any view.

    Solution: The right place to do this is func layoutSubviews()

    • layoutSubviews() gets called when the size of any subview will be changed or the frames of views/subviews gets updated first time.

    • This is the right place to add border or your view, and make sure it will only one call of adding border because layoutSubviews will get multiple calls if the frame of any subview will change.**

    Reference:
    https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews

    Login or Signup to reply.
  2. Much easier approach…

    Subclass UIView and put the border-related code inside it:

    class DashedView: UIView {
        
        let shapeLayer = CAShapeLayer()
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            commonInit()
        }
        required init?(coder: NSCoder) {
            super.init(coder: coder)
            commonInit()
        }
        
        func commonInit() -> Void {
            
            let color = UIColor(displayP3Red: 178.0/255.0, green: 181.0/255.0, blue: 200.0/255.0, alpha: 1.0).cgColor
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.strokeColor = color
            shapeLayer.lineWidth = 2
            shapeLayer.lineJoin = CAShapeLayerLineJoin.round
            shapeLayer.lineDashPattern = [6,3]
            
            layer.addSublayer(shapeLayer)
            
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()
            
            shapeLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 4).cgPath
            
        }
    
    }
    

    Now you don’t need any sort of view.addDashedBorder() calls.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search