iOS AutoLayout - get frame size width iOS AutoLayout - get frame size width ios ios

iOS AutoLayout - get frame size width


Actually, above answers are not quite right. I followed them and got zeros again and again.

The trick is to place your frame-dependent code to the viewDidLayoutSubviews method, which

Notifies the view controller that its view just laid out its subviews.

Do not forget that this method is called multiple times and is not the part of ViewController's life cycle, so be careful while using this.

Hope it will be helpful to someone.

Just wanted to add, that for my program without landscape mode NOT using Auto Layout is way much simplier... I tried, though =D


I think auto layout hasn't had the time to layout your views by the time you call this. Auto layout hasn't happened by the time viewDidLoad is called, because it's called just after the views are loaded and it's only after that that the views are placed into the view controller's view hierarchy and eventually laid out (in the view's layoutSubviews method).

Edit: this answer points out why the scenario in the question doesn't work. @dreamzor's answer points out where to place your code in order to solve it.


Include in your viewDidLoad()

self.view.setNeedsLayout()self.view.layoutIfNeeded()

Before accessing yourview.frame.size.width