7

我有一段适用于 iOS 7 以下的任何 iOS 的 iPad 应用程序代码

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 75, 44)];

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

UIBarButtonItem *composeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(toggleDelete:)];

[buttons addObject:composeButton];

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

fixedSpace.width = 5;

[buttons addObject:fixedSpace];

UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(touchMe:)];

[buttons addObject:bi];

[tools setItems:buttons animated:NO];

tools.barStyle = -1;

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

[bi release];
[fixedSpace release];
[composeButton release];
[buttons release];    
[tools release];

这个 pre iOS 7 的结果是:

在此处输入图像描述

在 iOS 7 上运行相同的代码会产生以下结果:

在此处输入图像描述

出于某种原因,这些按钮在 iOS 7 中被移到了工具栏的底部。

现在,我可以使用 UIBarItem imageInset 属性重新定位它们,但这似乎有点骇人听闻,因为我需要检查 iOS 版本,并且只有在 iPad 运行 iOS 7+ 时才执行 imageInset。我的问题是我是否遗漏了与 UIToolbar 相关的 iOS 7 特有的任何内容?我浏览了 iOS 7 UI 转换指南,找不到任何特定于这个问题的东西。

4

4 回答 4

5

由于我没有得到任何其他答案并为我找到了正确的解决方案,因此我将为遇到相同问题的其他人回答这个问题。如果您的目标是 iOS 5.0 及更高版本,有一种方便的方法可以将多个项目添加到右侧栏按钮。这是修复:

[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:deleteButton, bi, nil]];
于 2013-10-17T18:10:51.520 回答
5

检查设计器中的工具栏(故事板)。确保为工具栏上的所有按钮指定宽度。我有类似的问题,在我为情节提要的工具栏中的每个按钮指定宽度后,它已经消失了,现在按钮在 iOS 7 的工具栏中正确定位。

于 2013-10-18T16:06:51.120 回答
4

使用 CGRectZero 创建 UIToolbar 并在 setItems 之后设置它的框架:解决了我在 iOS 7 上的问题。

UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectZero];
//Create array with items
[tools setItems:buttonsArray animated:NO];
//Setting frame at this moment fixes the issue    
tools.frame = toolbarFrame;
于 2014-01-07T15:27:29.367 回答
0

我只有iOS7有这个问题。iOS8 总体上可以完美运行。解决办法是工具栏的高度为44.0,我设置了委托UIBarPositioning,项目位置为顶部:

- (UIBarPosition) positionForBar: (id<UIBarPositioning>) bar {
    return UIBarPositionTop;
}
于 2015-05-03T10:59:34.993 回答