Reputation: 7854
I have created a subclass of UIView
and called it MSMobileControlView
. I added a UIView
object to my Autolayout-enabled storyboard, and assigned it's class to MSMobileControlView
.
Inside MSMobileControlView
I have this code:
-(void)didMoveToSuperview
{
NSLog(@"self.frame: %@", NSStringFromCGRect(self.frame));
UIBUtton *levelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[levelButton setFrame:CGRectMake(0, 0, 150, 40)];
[levelButton setTitle:@"Next Level" forState:UIControlStateNormal];
[self addSubview:levelButton];
}
The NSLog
output is:
self.frame: {{0, 0}, {0, 0}}
However I can still see the view on the screen and the button responds to taps as expected. I have no other code associated with MSMobileControlView
.
What is going on?
Upvotes: 2
Views: 2279
Reputation: 1
You can check size in layoutSubviews delegate method within UIView class (called multiple times)
and override method didMoveToWindow and call super.didMoveToWindow() - it will trigger layoutSubviews after frames of all subviews would be correct
Upvotes: 0
Reputation: 26672
The Auto Layout runtime is assigning values to the frame later. If you want to see those assigned values, put your NSLog
in viewDidLayoutSubviews
.
If there's one piece of advice I would give you when starting out with Auto Layout, it is to forget frames. Just work with constraints. Let Auto Layout manipulate the frames on your behalf to give you the layout that your constraints specify.
Upvotes: 1