0

I am really getting tired of this:

I have a simple UIView called statesComboxBox. I have connected it on the IB using interface builder. I run the following line inside viewDidAppear method and it prints out zero.

NSLog(@"%f",self.statesComboBox.frame.size.width);

I am lost.. completely lost!

NOTE:

I am using Storyboards with Auto Layouts in iOS 6

john doe
  • 9,220
  • 23
  • 91
  • 167
  • 1
    Maybe `self.statesComboBox` is `nil`. –  May 10 '13 at 19:55
  • Just checked and it is not nil. – john doe May 10 '13 at 19:58
  • 1
    Hey, `bounds.size.width` and `frame.size.width` are two different things. What are you referring to? – mynk May 10 '13 at 20:08
  • If I put it in viewDidLayoutSubviews then works fine! – john doe May 10 '13 at 20:18
  • But viewDidLayoutSubviews fires a lot of times so I don't really want to do that! – john doe May 10 '13 at 20:26
  • It should probably be in viewDidLoad. – Mike C. May 10 '13 at 20:26
  • @MikeC. already mentioned that in viewDidLoad I do not get the bounds of the UIView. – john doe May 10 '13 at 20:29
  • Your question mentions viewDidAppear, not viewDidLoad. Actually viewDidAppear should be the right place imo. – nooitaf May 10 '13 at 20:37
  • I did it inside viewDidLayoutSubviews and it worked! But viewDidLayoutSubviews is fired many many times so I have to restrict doing something using if-else checks. – john doe May 10 '13 at 20:38
  • John, `viewWillAppear` is called when the view controller's view is added to the view hierarchy. It is possible that its subviews haven't loaded till then. However, `viewDidLayoutSubviews` is called when the view controller's view lay outs its subviews. The statesComboBox is a sub view. And since there is no method dedicated to appearance of EACH subview, you'll have to manage with `viewDidLayoutSubviews`. – mynk May 10 '13 at 20:58
  • 2
    The problem is that you're using autolayout, which calculates dynamically the sizes based on given constraints. You can find [a similar answer here](http://stackoverflow.com/questions/12527191/ios-autolayout-get-frame-size-width) (2nd one) and a brief guide (among many others on the web) [here](http://oleb.net/blog/2013/03/things-you-need-to-know-about-cocoa-autolayout/) – Alladinian May 10 '13 at 21:01
  • This shouldn't happen even with auto layout. If you're logging this in viewDidAppear, it should work. I've done this many times, and it's always worked fine in viewDidAppear. There must be something else going on. Try logging the whole frame and see if it all logs as zero -- NSLog(@"%@",NSStringFromCGRect(self.statesComboBox.frame)); – rdelmar May 10 '13 at 22:09
  • @rdelmar I logged in viewDidAppear and it shows everything as 0. Bounds is 0 and frame is also zero. – john doe May 13 '13 at 00:33
  • And if you log self.statesComboBox it's not nil? – rdelmar May 13 '13 at 00:34

0 Answers0