title: Swift callback function for draw()?
body: <p>I have a <code>customView</code> where I overwrite <a href="https://developer.apple.com/documentation/uikit/uiview/1622529-drawrect" rel="nofollow noreferrer"><code>drawRect:</code></a>. I also have some cases, where I want to display a UILabel ontop of my <code>customView</code>:</p>
  
  <pre><code>if self.ticks.count &lt; 50 {
      self.label.frame = self.bounds
      self.label.text = "This chart requires 50 or more ticks.\nKeep Ticking."
      self.addSubview(label)
  }
  </code></pre>
  
  <p>However if I call this code before the <a href="https://developer.apple.com/documentation/uikit/uiview/1622529-drawrect" rel="nofollow noreferrer"><code>drawRect</code></a> is called, then my label doesn't show up. Is there a callback method like <code>viewDidDraw</code> or something?</p>
  
  <h2>EDIT</h2>
  
  <p>Someone asked for it, so here is all the code that is now doing what I need it to do. I think the answer to the question is to override <code>layoutSubviews</code>.</p>
  
  <pre><code>@IBDesignable open class  CustomView: UIView {
      @objc open var ticks:[Tick] = []
      let label = UILabel()
  
      required public init?(coder: NSCoder) {
          super.init(coder: coder)
          setup()
      }
      required public override init(frame: CGRect) {
          super.init(frame: frame)
          setup()
      }
      func setup(){
          self.backgroundColor = UIColor.clear
          self.label.frame = self.bounds
          self.label.text = "This chart requires 50 or more ticks.\nKeep Ticking."
          self.label.numberOfLines = 0
          self.label.textAlignment = .center
          self.addSubview(label)
      }
  
      override open func layoutSubviews() {
          self.label.frame = self.bounds
      }
  
      @objc open func setSessions(sessions:[Session]){
          self.ticks = []
          for session in sessions.reversed(){
              if self.ticks.count &lt; 250{
                  self.ticks = self.ticks + (session.getShots() as! [Shot])
              }else{
                  break
              }
          }
          if self.ticks.count &lt; 50 {
              self.label.isHidden = false
          }else{
              self.label.isHidden = true
          }
          self.bringSubview(toFront: self.label)
      }
  
      override open func draw(_ rect: CGRect) {
          let context = UIGraphicsGetCurrentContext()!
          let width = rect.width - 10;
          let height = rect.height - 10;
          let center = CGPoint(x: width/2+5, y: height/2+5)
          let big_r = min(width, height)/2
  
          context.setLineWidth(1.5)
          UIColor.white.setStroke()
  
          //draw the rings
          for i in stride(from: 1, to: 7, by: 1){
              let r = big_r * CGFloat(i) / 6.0;
              context.addArc(center:center, radius:r, startAngle: 0, endAngle: CGFloat(2*Double.pi), clockwise: false)
              context.strokePath()
          }
  
          //draw the ticks
          if self.ticks.count &gt; 49 {
              UIColor.red().setFill()
              for (i, tick) in self.ticks.prefix(250).enumerated(){
                  let radius = min((100.0-CGFloat(shot.score))/30.0 * big_r, big_r);
  
                  let x = Utilities.calculateX(tick, radius)
                  let y = Utilities.calculateY(tick, radius)
  
                  let point = CGPoint(x: x+center.x,y: y+center.y)
                  context.addArc(center:point, radius:CGFloat(DOT_WIDTH/2), startAngle: 0, endAngle: CGFloat(2*Double.pi), clockwise: false)
                  context.fillPath()
              }
          }
      }
  }
  </code></pre>