Reputation: 101
I have a UIView whose subview does not want to completely fill self.view after rotating into landscape mode. I have tried many things from setting autoresize masks to setting the frames of subviews after rotation, they either don't work, or the problem becomes much worse. Here is an example with a UINavigationBar that experiences this problem (navBar does not completely fill landscape width). The code:
- (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor blueColor];
myBar =[[UINavigationBar alloc] init];
myBar.frame = CGRectMake (0, 0, self.view.frame.size.width, 44);
UINavigationItem *navItem =[[[UINavigationItem alloc] initWithTitle:@"Title"] autorelease];
UIBarButtonItem *rightButton =[[[UIBarButtonItem alloc] initWithTitle: @"Email" style: UIBarButtonItemStylePlain target: self action:@selector (rightButtonPressed)] autorelease];
navItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton =[[[UIBarButtonItem alloc] initWithTitle: @"About" style: UIBarButtonItemStylePlain target: self action:@selector (leftButtonPressed)] autorelease];
navItem.leftBarButtonItem = leftButton;
myBar.barStyle = UIBarStyleDefault;
myBar.items =[NSArray arrayWithObject:navItem];
[self.view addSubview:myBar];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
@end
Upvotes: 0
Views: 371
Reputation:
Just add a single line of code at the end of your code:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor blueColor];
UINavigationBar *myBar;
myBar =[[UINavigationBar alloc] init];
myBar.frame = CGRectMake (0, 0, self.view.frame.size.width, 44);
UINavigationItem *navItem =[[UINavigationItem alloc] initWithTitle:@"Title"];
UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithTitle: @"Email" style: UIBarButtonItemStylePlain target: self action:@selector (rightButtonPressed)];
navItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton =[[UIBarButtonItem alloc] initWithTitle: @"About" style: UIBarButtonItemStylePlain target: self action:@selector (leftButtonPressed)];
navItem.leftBarButtonItem = leftButton;
myBar.barStyle = UIBarStyleDefault;
myBar.items =[NSArray arrayWithObject:navItem];
[self.view addSubview:myBar];
[myBar setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
I removed autorelease because I used ARC. You can add autorelease again. Have a good day friend.
Upvotes: 1