横屏 iPhone 上 UIBarButtonItem 的高度

9

我在iPhone应用中有一个导航栏,其中包含自定义的UIBarButtonItems:

UIBarButtonItem* homePersonButton = [[UIBarButtonItem alloc]
       initWithImage:[UIImage imageNamed:@"homePerson.png"]
               style:UIBarButtonItemStylePlain
              target:self action:@selector(showHomePerson)];

homePerson.png的尺寸为20 x 18。

在竖屏模式下看起来很好,但是在横屏模式下,按钮上的20x18的图像太高了,不太好看。看起来iOS会对他们的标准按钮进行一些处理,以便在较窄的导航栏中缩小图像。

最佳解决方案是什么?

另一个例子是当我有一个自定义按钮在导航栏的titleView中时。我需要它在应用程序旋转时也能缩小。

非常感谢您能给我任何指导!


3
iOS5用户提示:现在有一个[[UIBarButtonItem alloc] initWithImage:<#(UIImage *)#> landscapeImagePhone:<#(UIImage *)#>...方法。 - steipete
我的项目在导航栏中,因此它们的样式始终为UIBarButtonItemStyleBordered,除非我使用自定义视图创建它们。在这种情况下,是否有一种方法可以使我的自定义视图也调整为18x18像素,就像系统项目一样? - nacho4d
3个回答

5

工具栏和导航栏图片的大小为20 x 20,因此如果提供的图像不符合这些尺寸,则必须由框架调整/缩放您的图像,这会导致问题。请将您的图像调整/缩放为20x20,这样您的图像应该在纵向和横向都显示正确。


谢谢你的帮助。我的图像现在是20x20,但它仍然不能像系统按钮图像一样缩放。所以我还是有同样的问题。它在较细的导航栏上看起来太大了。 - toofah
1
你尝试过在检测到方向更改时手动调整/重新调整图像的大小,或更换为具有特定横向版本的图像吗? - Kevin McMahon
看起来你必须像建议的那样手动完成这个任务。太糟了。 - toofah

3

steipete在评论中提到了这一点,但我觉得现在需要成为一个答案。从iOS 5开始,您可以使用UIBarButtonItem initializer

- initWithImage:landscapeImagePhone:style:target:action:

只需将landscapeImagePhone图像设置为您想要在横向调整大小的图像的较小版本。

经过一些测试,我注意到iPhone确实会缩小导航栏和导航栏上的UIBarButtonItems,但是iPhone 6 Plus不会。 6 Plus似乎保持导航栏高度为44px,而常规iPhone将其缩小为32px。 因此,6 Plus在横向模式下也不会使用landscapeImagePhone

我通过不仅使用landscapeImagePhone的较小尺寸图像,而是完全不同的图像来测试这一点。 在6 Plus上,图像从未改变过。


然而,从iOS 9.2开始,在iPhone 6 Plus上要注意以下内容:

Apple文档中对于此初始化程序的说明是landscapeImagePhone是:

在UIUserInterfaceIdiomPhone设备中横向栏中使用的项目图像。

iOS 9.2之后,6 Plus在纵向和横向上都报告为UIUserInterfaceIdiomPhone。在此之前,6 Plus曾经是UIUserInterfaceIdiomUnspecified。我不确定这是什么时候更新的。但是,由于现在它是UIUserInterfaceIdiomPhone,根据文档,landscapeImagePhone也应该适用于6 Plus。

也许它并不是那么依赖UIUserInterfaceIdiom,而更多地依赖于导航栏的高度。我不确定。只需注意这种不一致性可能会在未来被“修复”,从而导致不同的行为。

0

我有类似的问题。现在我使用自定义类型到按钮上。例如,这是我的UIBarButtonItem目录。 你可以使用它来构建一个自定义的导航栏按钮。

+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image highlightedImage:(UIImage *)highlightedImage     target:(id)target action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
    button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    return barItem;
}

当你旋转时,可以调整按钮自定义视图的框架。

提示:

  1. 在旋转方法中,例如willAnimateRotationToInterfaceOrientation,打印导航栏按钮项的自定义视图框架,并获取您想要的正确框架(在纵向模式下),同时,您会发现横向模式下框架不正确,需要进行修复。
  2. 通过引用正确的框架值来解决横向栏按钮框架问题。

在我的情况下,我只是简单地修复了自定义视图的原点...例如:

// adjust the navigation Item top, otherwise it is not proper set after rotation
_centralRootViewController.navigationItem.leftBarButtonItem.customView.top = 5;
_centralRootViewController.navigationItem.rightBarButtonItem.customView.top = 5;

注意:top 是框架的 y 坐标。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接