title: How to programmatically create UIlabel inside a custom uiView swift
body: <p>how can i create 5 custom uilabel inside a custom <code>UIView</code>. Which will align one after the other. i have tried to create the <code>UILabel</code> inside the custom <code>UIView</code>. but it does not go inside the custom <code>UIView</code>. </p>
  
  <p>//updated with the loop</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
  @IBOutlet weak var mainView: UIView!
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  
      var getMainViewX = mainView.frame.origin.x
      let getMainViewY = mainView.frame.origin.y
  
      for var i = 0; i &lt; 5; i++
      {
  
          let label = UILabel(frame: CGRectMake(getMainViewX, getMainViewY, 200, 21))
          //label.center = CGPointMake(160, 284)
          label.textAlignment = NSTextAlignment.Center
          label.text = "I'am a test label"
          self.mainView.addSubview(label)
          getMainViewX+=20
      }
  
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/0YdkY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0YdkY.png" alt="the light gray box in the middle is the uiview"></a></p>