7

我正在开发一个可以在两个方向上工作的 iPhone 应用程序:纵向和横向。

我正在使用嵌入在 UINavigationController 中的一个视图和表格视图。此导航栏及其按钮的高度是: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];   

替代文字 http://labs.kiesl.eu/images/navbar.png

谢谢

汤姆

4

3 回答 3

7

我想通了:导航栏的高度一定是32px!使用 33 或 34 px 时,对齐会搞砸。

于 2009-10-26T08:42:43.520 回答
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];
}
于 2012-01-04T14:37:36.020 回答
0

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

于 2009-10-25T15:40:50.447 回答