title: Drawing a dotted background in swift
body: <p>Im trying to achieve a dotted view that I can use as a background. Something looking a bit like this:
  <a href="https://i.stack.imgur.com/PcTte.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PcTte.png" alt="Image showing dotted background"></a></p>
  
  <p>But I seem to be missing something in my code. I have tried to lab around with different sizes of the dots and everything, but so far I'm just getting my background color set to the view but no dots no matter the size, the color or the spacing. What am I missing?</p>
  
  <pre><code>class DottedBackgroundView: UIView {
  
  override func draw(_ rect: CGRect) {
      guard let context = UIGraphicsGetCurrentContext() else { return }
      UIColor.green.setFill()
      context.fill(rect)
  
      let drawPattern: CGPatternDrawPatternCallback = { _, context in
          context.addArc(
              center: CGPoint(x: 5, y: 5), radius: 2.5,
              startAngle: 0, endAngle: CGFloat(2.0 * .pi),
              clockwise: false)
          context.setFillColor(UIColor.white.cgColor)
          context.fillPath()
      }
  
      var callbacks = CGPatternCallbacks(
          version: 0, drawPattern: drawPattern, releaseInfo: nil)
  
      let pattern = CGPattern(
          info: nil,
          bounds: CGRect(x: 0, y: 0, width: 10, height: 10),
          matrix: .identity,
          xStep: 10,
          yStep: 10,
          tiling: .constantSpacing,
          isColored: true,
          callbacks: &amp;callbacks)
  
      let patternSpace = CGColorSpace(patternBaseSpace: nil)!
      context.setFillColorSpace(patternSpace)
  
      var alpha: CGFloat = 1.0
      context.setFillPattern(pattern!, colorComponents: &amp;alpha)
      context.fill(rect)
  
      context.addArc(
          center: CGPoint(x: 5, y: 5), radius: 5.0,
          startAngle: 0, endAngle: CGFloat(2.0 * .pi),
          clockwise: false)
      }
  }
  </code></pre>