6

I'm re-writting an app under IOS6 with autolayout but have problems accessing the size of a subclass of UIView

@property (weak, nonatomic) IBOutlet MyView *myView1;

in

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"myView1: %@",self.myView1);
}

The size of the UIView is fully defined in the StoryBoard but comes out as zero when I run the code. Is this todo with auto layout or is the subview simply not defined yet? Am I doing something basically wrong here?

drw
  • 943
  • 1
  • 10
  • 25

2 Answers2

11

Just found one possible answer here. Running the methods

[self.view setNeedsLayout];
[self.view layoutIfNeeded];

forces a calculation of UIViews dimensions causing the expected values to appear in the log and also be known unto the UIView subclass itself.

Community
  • 1
  • 1
drw
  • 943
  • 1
  • 10
  • 25
0

I just printed the frame of a subview.

It was 0 in viewDidLoad.

It had a non-zero value inside the viewDidLayoutSubviews callback.

Inside the viewDidLoad you should only expect that the value of the viewcontroller's view to be non-zero. Inside viewDidLoad is actually where sometimes you add subviews and stuff. So it's not correct to expect the frame to get calculated in the very callback that you're settings constraints for it.

Basically what needs to be understood is that first a viewController is drawn, then as it goes through its life cycle events, its subviews will get laid out

mfaani
  • 33,269
  • 19
  • 164
  • 293