7

我正在研究可在两种方向(纵向和横向)下工作的iPhone应用程序。iPhone:带按钮的UINavigationBar - 调整高度

我使用嵌入在UINavigationController中的一个视图和tableview。此导航栏及其按钮的高度为:44px纵向或34px横向。

在不同的视图中,我自己创建了UINavigationBar,并且能够设置正确大小的框架,但嵌入的带有UIBarButtonItem的UINavigationItem不会收缩。因此,对于风景模式下的34像素,此按钮很大并且与高度上的导航栏重叠。

有趣的是,这与其他应用程序中的相同代码一起工作......不知道它不在这里。

有无论如何调整UIBarButtonItem的高度/位置吗?

这里是代码片段:

navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 34.0f)]; 
[navBar setBarStyle: UIBarStyleBlackOpaque]; 

[self addSubview: navBar]; 

barButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"flip", @"flip") style:UIBarButtonItemStylePlain target:self action:@selector(flip)]; 

item = [[UINavigationItem alloc] initWithTitle: NSLocalizedString(@"Translation", @"Translation Tab Bar Title")]; 
[item setRightBarButtonItem: barButton]; 
[navBar pushNavigationItem:item animated:NO]; 

alt text http://labs.kiesl.eu/images/navbar.png

由于

汤姆

回答

0

创建备用图像,并且在该处理动画的旋转视图控制器的方法,改变图像栏中的按钮项目。或者,您可以使用UIImageView作为项目中的自定义视图,并设置其内容模式以缩小图像。

7

我想通了:导航栏的高度必须是32像素!用33或34像素对齐螺丝向上。

+0

非常有帮助,谢谢。 32px是关键。我使用的是34像素,它不起作用。 – lambmj 2012-01-04 14:33:58

4

这是我写的基于crashtesttommy的答案代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

- (void) correctNavBarHeightForOrientation:(UIInterfaceOrientation)orientation { 
    // This is only needed in on the iPhone, since this is a universal app, check that first. 
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone){ 
     if (UIInterfaceOrientationIsLandscape(orientation)) { 
      self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 32.0f); 
     } else { 
      self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 44.0f); 
     } 
    } 
} 

- (void) viewWillAppear:(BOOL)animated { 
    [self correctNavBarHeightForOrientation:[UIApplication sharedApplication].statusBarOrientation]; 
} 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [self correctNavBarHeightForOrientation:toInterfaceOrientation]; 
}