在我的应用程序中,我有一个带有 3 个按钮的 uinavigationbar,但我用于 ios5 的代码似乎不再适用于 ios6。 您可以在我的自定义 View 下方看到一个颜色错误的矩形,右侧有两个按钮。
这是我使用的代码,我在 stackoverflow 上找到了它:
UIToolbar *tools = [[[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 110.0f, 44.01f)] autorelease]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = NO;
tools.translucent = self.headerbar.translucent;
tools.barStyle = self.headerbar.barStyle;
tools.backgroundColor = self.headerbar.backgroundColor;
tools.tintColor = self.headerbar.tintColor;
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create a standard refresh button.
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithTitle:@"heute" style:UIBarButtonItemStylePlain target:self action:@selector(showThisWeek)];
bi.style = UIBarButtonItemStyleBordered;
bi.width = 0;
[buttons addObject:bi];
//[bi release];
// Create a spacer.
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(showThisWeek)];
[buttons addObject:bi];
//[bi release];
// Add profile button.
bi = [[UIBarButtonItem alloc] initWithTitle:@"vor" style:UIBarButtonItemStylePlain target:self action:@selector(showNext)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
//[bi release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
//[tools release];
header.rightBarButtonItem = twoButtons;
[header.leftBarButtonItem setAction:@selector(showPrevious)];
有人可以提示我一个在 ios5 和 ios6 上都能正常工作的更优雅的解决方案吗?
干杯 卢卡斯
最佳答案
您尝试过 leftBarButtonItems 和 rightBarButtonItems 吗?您可以使用它在左侧和右侧导航栏中添加按钮数组。
如果你使用这个,就不需要 UIToolbar 及其 subview 。
关于ios6 UINavigationBar 超过 2 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672985/