景观iPhone中的Uibarbuttonitem高度
-
30-09-2019 - |
题
我在iPhone应用程序中有一个带有自定义UibarbuttonItems的NAV栏:
UIBarButtonItem* homePersonButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"homePerson.png"]
style:UIBarButtonItemStylePlain
target:self action:@selector(showHomePerson)];
主人png是20 x 18。
它在肖像模式下看起来不错,但是在景观模式下,按钮上的20x18图像太高了,看起来不错。看起来iOS在其标准按钮上进行了一些工作,以在较薄的NAV栏中根据需要缩小图像。
处理此问题的最佳方法是什么?
另一个示例是,当我在导航栏的标题视图中有一个自定义按钮时。当应用旋转时,我也需要它也会收缩。
预先感谢您的任何指示,您可以给我!
解决方案
工具栏和导航栏映像的大小为20 x 20,因此,当您提供不符合这些维度的图像时,您将留在框架上,以调整/扩展您的图像大小,从而导致您的问题。调整/缩放图像到20x20,您的图像在肖像和景观中都应看起来正确。
其他提示
Steipete在评论中提到了这一点,但我觉得现在需要答案。从iOS 5开始,您可以使用 UibarbuttonItem初始化器:
- 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 Idiom中的景观栏中的项目。
从iOS 9.2开始,6 Plus确实报告为 UIUserInterfaceIdiomPhone
在肖像和景观中。以前,6 plus曾经是 UIUserInterfaceIdiomUnspecified
. 。我不确定何时更新。但是,因为现在是 UIUserInterfaceIdiomPhone
, ,根据文档, landscapeImagePhone
也应适用于6 plus。
因此,也许它不像在导航栏的高度上那样依赖UiuserinterfaceidioM。我不确定。请注意,这种不一致可能会在将来“固定”,因此会导致不同的行为。
我有类似的问题。现在,我使用自定义类型到按钮。例如,这是我的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;
}
然后,您可以在旋转时调整Bar按钮自定义视图框架。
提示:
- 在旋转方法中,例如
willAnimateRotationToInterfaceOrientation
, ,打印导航条按钮项的自定义视图框架,并获取所需的正确框架(以狭窄模式),此外,您还可以看到框架在景观模式下是不正确的,我们需要对其进行修复。 - 通过引用正确的框架值来修复景观条按钮框架问题。
就我而言,我只是简单地修复了自定义视图的来源...
// 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;
注意:顶部是框架的y。