title: Access UIView width at runtime
body: <p>In Swift, how do I get the width (or height) at runtime, of a UIView  that's created programmatically using auto layout constraints?</p>
  
  <p>I've tried <code>view1.frame.size.width</code> and <code>view1.frame.width</code>, but both return <code>0</code>.</p>
  
  <p>I've been following an auto layout tutorial by <a href="http://makeapppie.com/2014/07/26/the-swift-swift-tutorial-how-to-use-uiviews-with-auto-layout-programmatically/" rel="noreferrer">MakeAppPie</a> and added <code>view1.layer.cornerRadius = view1.bounds.width/2</code> as below, but this had no effect, so I did a po on  <code>view1.bounds.width</code> and got <code>0</code>:</p>
  
  <pre><code>class ViewController: UIViewController {
  
  func makeLayout() {
  
      //Make a view
      let view1 = UIView()
      view1.setTranslatesAutoresizingMaskIntoConstraints(false)
      view1.backgroundColor = UIColor.redColor()
  
  
      //Make a second view
      let view2 = UIView()
      view2.setTranslatesAutoresizingMaskIntoConstraints(false)
      view2.backgroundColor = UIColor(red: 0.75, green: 0.75, blue: 0.1, alpha: 1.0)
  
      //Add the views
      view.addSubview(view1)
      view.addSubview(view2)
  
  
      //--------------- constraints
  
      //make dictionary for views
      let viewsDictionary = ["view1":view1,"view2":view2]
  
      //sizing constraints
      //view1
      let view1_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[view1(&gt;=50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
      let view1_constraint_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[view1(50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
  
      view1.addConstraints(view1_constraint_H)
      view1.addConstraints(view1_constraint_V)
  
      //view2
      let view2_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:[view2(50)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
      let view2_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:[view2(&gt;=40)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
  
      view2.addConstraints(view2_constraint_H)
      view2.addConstraints(view2_constraint_V)
  
      //position constraints
  
      //views
      let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-100-[view2]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
      let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-136-[view1]-100-[view2]-100-|", options: NSLayoutFormatOptions.AlignAllTrailing, metrics: nil, views: viewsDictionary)
  
      view.addConstraints(view_constraint_H)
      view.addConstraints(view_constraint_V)
  
      view1.layer.cornerRadius = view1.bounds.width/2
  
      }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      func supportedInterfaceOrientations() -&gt; Int {
          return Int(UIInterfaceOrientationMask.All.rawValue)
  
      }
  
      // Do any additional setup after loading the view, typically from a nib.
  
      view.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 1, alpha: 1.0)
      makeLayout()
  
      }
  }
  </code></pre>