-->

iPhone:UINavigationBar的带按钮 - 调整高度(iPhone: UINaviga

2019-09-01 01:43发布

我工作的一个iPhone应用程序,它在两个方向工作:人像和风景。

我使用一个视图和tableview中嵌入的UINavigationController。 这个导航栏及其按钮的高度可以是:44px纵向或横向34像素。

在一个不同的观点我自己创造的UINavigationBar的,我能够设置框架正确的尺寸,但随着的UIBarButtonItem嵌入式UINavigationItem不缩水。 因此,对于在风景模式下的34 PX这个按钮是大,重叠高度导航栏。

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

反正是有一个调整的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

谢谢

汤姆

Answer 1:

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



Answer 2:

这是我写的基于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];
}


Answer 3:

创建备用图像,并且在该手柄动画旋转视图控制器的方法,在栏按钮项改变图像。 或者,你很可能在一个UIImageView为您的项目自定义视图,并设置其内容的模式,缩小图片。



文章来源: iPhone: UINavigationBar with buttons - adjust the height