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

发布于 2024-08-09 01:06:52 字数 1055 浏览 7 评论 0原文

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

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

在不同的视图中,我自己创建了 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];   

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

谢谢

汤姆

I am working on an iPhone application which works in both orientations: portrait and landscape.

I am using for one view and tableview embedded in an UINavigationController. The height of this navigationbar with its buttons is either: 44px portrait or 34px landscape.

Within a different view I created the UINavigationBar by myself and I am able to set the frame for the correct size, but the embedded UINavigationItem with UIBarButtonItem doesn't shrink. So for the 34 px in the landscape mode this button is to big and overlaps the navbar in the height.

The funny thing is though, that this worked with identical code in other apps... have no idea which it isn't here.

Is there anyway to adjust the height / position of an UIBarButtonItem?

Here is the code snippet:

    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

Thanks

Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恬淡成诗 2024-08-16 01:06:56

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

Create alternate images, and in the view controller methods that handle animating the rotation, change the images in the bar button items. Or you could probably use a UIImageView as a custom view in your item, and set its content mode to scale the image down.

清欢 2024-08-16 01:06:55

这是我根据 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];
}

Here's the code I wrote based on crashtesttommy's answer:

- (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];
}
征棹 2024-08-16 01:06:52

我想通了:导航栏的高度必须是32px! 33 或 34 px 时,将对齐螺丝拧上。

I figured it out: The height of the navigation bar must be 32 px! With 33 or 34 px the alignment screws up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文