2013-03-02 47 views
0

我有一个UIView的子视图不想完全填充self.view后旋转到横向模式。我已经尝试了很多东西,从设置自动调整大小掩码到旋转后设置子视图的帧,它们要么不工作,要么问题变得更糟。下面是一个遇到这个问题的UINavigationBar的示例(navBar不会完全填充横向宽度)。代码:风景SubViews不填充self.view

- (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 

回答

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。你可以再次添加autorelease。祝你有美好的一天。

+0

谢谢你,这个伎俩! – s1ris 2013-03-02 04:39:52