Kevin Sylvestre
Kevin Sylvestre

Reputation: 38052

UIView Not Respecting Frame in 'loadView' in UINavigationController

I have the following snippet of code:

- (void)loadView {
    self.view = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 20.0, 40.0, 40.0)];
    self.view.backgroundColor = [UIColor blackColor];
}

If I run the snippet in a project created using the 'view-based application' in the main view controller everything works as expected. However, If I run it in a project created using a 'navigation-based application' the label fills the screen (does not respect the sizes). How can I fix the behaviour for presenting when in a navigation controller? Thanks!

Upvotes: 2

Views: 581

Answers (1)

tia
tia

Reputation: 9698

Navigation view controller will resize the view of the view controller to the display area, so the behavior you describe is normal and by-design. Generally, we use empty UIView at the top of hierarchy, and put the subviews inside, which is UILabel in your case.

Upvotes: 1

Related Questions