title: How to get sizes of screen elements dynamically in swift ios?
body: <p>Sorry if my question is rather stupid: I am currently working on screen elements and need to get their sizes dynamically, depending on the size of the parent view. </p>
  
  <p>If I use frames I need to get fixed sizes, right? For example:</p>
  
  <pre><code>let object = UIView(frame: CGRect(x: 20 + ( (10+34)*nrX ), 
    y: 10+(10+34)*nrY, width: 34, height: 34))
  </code></pre>
  
  <p>Of course, I can tune the sizes manually, but they remain fixed.</p>
  
  <p>Is there any possibility to get the size of the elements and the distances between them dynamically? For example: Could each element have a size of 1/10 of the length of the parent view and 1/10 of the width?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
  @IBOutlet weak var testLabel: UILabel!
  
  let testSubView = UIView()
  var allObjects = [UIView]()
  
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  
      createTestSubview()
  
      testLabel.text = "Elements: \(allObjects.count)"
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  func createTestSubview(){
  
  
          // Creating testSubView
  
          testSubView.backgroundColor = UIColor.purple //For test purpose
  
          testSubView.layer.borderWidth = 0
  
          testSubView.layer.cornerRadius = 5
          testSubView.layer.masksToBounds = true
  
          testSubView.translatesAutoresizingMaskIntoConstraints = false //Don't forget this line
          view.addSubview(testSubView)
  
  
  
          let horizontalConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)
          view.addConstraint(horizontalConstraint)
  
          let verticalConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)
          view.addConstraint(verticalConstraint)
  
          let widthConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.width, multiplier: 0.7, constant: 0)
          view.addConstraint(widthConstraint)
  
          let heightConstraint = NSLayoutConstraint(item: testSubView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: view, attribute: NSLayoutAttribute.height, multiplier: 0.7, constant: 0)
          view.addConstraint(heightConstraint)
  
      createScreen()
  
  }
  
  
  func createObject(nrX:Int,nrY:Int,tagNr:Int) {
  
  
  
      let object = UIView(frame: CGRect(x: 10 + ( (10+20)*nrX ), y: 10+(10+20)*nrY, width: 20, height: 20))
      object.backgroundColor = UIColor.yellow //For test purpose
  
      object.tag = tagNr
  
      object.translatesAutoresizingMaskIntoConstraints = false
      testSubView.addSubview(object)
      allObjects.append(object)
  
  }
  
  func createScreen(){
  
      var count = 0
      let colum = 7
  
      let rows  = 7
      for j in 0..&lt;rows{
          for i in 0..&lt;colum{
              if count &lt; 49 {
                  createObject(nrX: i,nrY:j,tagNr:count)
                  count += 1
              }
          }
      }
  
  }
  </code></pre>