横向子视图不填充self.view



我有一个UIView,它的子视图不想完全填充self。旋转到横向模式后的视图。我尝试了很多事情,从设置自动调整大小蒙版到设置旋转后的子视图框架,它们要么不起作用,要么问题变得更糟。下面是一个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

只需在代码末尾添加一行代码:

- (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。您可以再次添加自动释放。祝你有个美好的一天。

最新更新