Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS AutoLayout - get frame size width

I am developing using iOS 6 auto layout

I would like to log a message displaying the frame width of the view.

I can see the textView on the screen.

But I get the width and height as zero, am I missing something ?

NSLog(@"textView    = %p", self.textView); NSLog(@"height      = %f", self.textView.frame.size.height); NSLog(@"width       = %f", self.textView.frame.size.width);  textView    = 0x882de00 height      = 0.000000 width       = 0.000000 
like image 577
user1046037 Avatar asked Sep 21 '12 08:09

user1046037


1 Answers

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

like image 132
dreamzor Avatar answered Sep 21 '22 08:09

dreamzor