我有一个UIView,其子视图在旋转到横向模式后不想完全填充self.view。我已经尝试了很多东西,从设置自动调整大小的掩码到设置旋转后的子视图帧,它们要么不起作用,要么问题变得更糟。下面是一个UINavigationBar的例子,它遇到了这个问题(navBar没有完全填充横向宽度)。代码:
- (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
答案 0 :(得分:1)
只需在代码末尾添加一行代码:
- (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];
}
我删除了自动释放,因为我使用了ARC。您可以再次添加自动释放。有一个美好的一天朋友。