title: Programmatically creating buttons at different locations
body: <p>I programmatically created UIButtons and for them to appear at different locations on the screen. I tried to prevent collisions however they still occur in some instances during testing. The code I'm using is below, any additional logic I have to consider? </p>
  
  <pre><code>class PlayViewController: UIViewController {
  
  override func viewDidLoad() {
      super.viewDidLoad()
      let screenSize: CGRect = UIScreen.mainScreen().bounds
  
      let buttonWidth = screenSize.width * 0.15;
      let buttonWidth1 = screenSize.width * 0.20;
  
      var xPos:[Int] = []
      var yPos:[Int] = []
  
      for (var i = 0; i&lt;4; i++) {
  
          var xTemp = Int(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))
          while xPos.contains((xTemp)) {
              xTemp = Int(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))
  
          }
  
          var yTemp = Int(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))
  
          while yPos.contains(yTemp) {
              yTemp = Int(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))
          }
  
          let xRand = CGFloat(xTemp)
  
          var yRand = CGFloat(yTemp)
          if yRand &lt;= buttonWidth1 {
              yRand += buttonWidth1
          }
  
          let but1 = UIButton(frame: CGRect(x: xRand, y: yRand, width: buttonWidth, height: buttonWidth))
          but1.setTitle(String(i), forState: .Normal)
      but1.setTitleColor(UIColor.blueColor(), forState: .Normal)
      but1.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
  
  but1.layer.borderWidth = 2.0
      but1.layer.borderColor = UIColor.blackColor().CGColor
  
          self.view.addSubview(but1)
  
          var x = 0
          while (x &lt;= Int(buttonWidth1)) {
              xTemp += 1
              xPos.append(xTemp)
              x++;
          }
          var y = 0
          while (y &lt;= Int(buttonWidth1)) {
              yTemp += 1
              yPos.append(yTemp)
              y++;
          }
      }
      var g = 0
  
      // Do any additional setup after loading the view.
  }
  </code></pre>
  
  <p>UPDATED CODE</p>
  
  <pre><code>var intersection:[CGRect] = []
  
  for (var i = 0; i&lt;x; i++) {
  
      var xTemp = CGFloat(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))
      var yTemp = CGFloat(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))
  
      if yTemp &lt;= buttonWidth1 {
          yTemp += buttonWidth1
      }
  
      var rect1 = CGRect(x: xTemp, y: yTemp, width: buttonWidth, height: buttonWidth)
      for element in intersection {
              while CGRectIntersectsRect(rect1, element) {
                  xTemp = CGFloat(arc4random_uniform(UInt32(screenSize.width - buttonWidth1)))
                  yTemp = CGFloat(arc4random_uniform(UInt32(screenSize.height - buttonWidth1)))
  
                  if yTemp &lt;= buttonWidth1 {
                      yTemp += buttonWidth1
                  }
  
                  rect1 = CGRect(x: xTemp, y: yTemp, width: buttonWidth, height: buttonWidth)
              }
      }
  
      let but1 = UIButton(frame: rect1)
      but1.setTitle(String(i), forState: .Normal)
      but1.setTitleColor(UIColor.blackColor(), forState: .Normal)
      but1.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
      but1.layer.borderWidth = 2.0
      but1.layer.borderColor = UIColor.blackColor().CGColor
  
      self.view.addSubview(but1)
  
      intersection.append(rect1)
  }
  </code></pre>