如何在UINavigationItem.rightBarButtonItem(或leftBarButtonItem)上添加多个UIBarButton?

15
我已经尝试了这种方法/技巧:http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/ 问题是这会留下微弱的接缝。我尝试将嵌套工具栏的背景图片设置为我捕捉到的应该是什么的图像,但是没有起作用。我还尝试过使用嵌套的UINavigationBar,但似乎也不行。
我在几个iPhone应用程序中看到过这样做。有人知道怎么做吗?
[编辑] 我想要按钮看起来像普通的UIBarButtonItems,并且能够使用系统样式,如UIBarButtonSystemItemAdd、UIBarButtonSystemItemRefresh。我提供的链接可以做到这一点,只是因为它是嵌套在导航栏中的UIToolbar,所以你可以看到微弱的接缝。
请不要提及这违反了人机界面指南。(我们知道)。
感谢您贡献您的技巧...那是唯一的方法!

你提到的链接已经失效了 :-( - Living Skull
8个回答

36

iOS 5.0现在支持多个按钮。请参阅UINavigationItem的iOS文档。具体而言,如下所示:

属性:

@property(nonatomic, copy) NSArray *leftBarButtonItems;
@property(nonatomic, copy) NSArray *rightBarButtonItems;
@property BOOL leftItemsSupplementBackButton;

方法:

- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated;
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;


6

2
FYI,iOS 5.0 支持多个导航栏按钮。请查看我下面发布的答案。但是,如果您需要部署到较旧版本的操作系统,则仍需要使用本答案中描述的技术。 - Craig B

3
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myWidth, myHeight)];
// make UIView customView1... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView1];
[customView1 release];
// make UIView customView2... (UILabel, UIButton, etc.) with desired frame and settings
[parentView addSubview:customView2];
[customView2 release];
UIBarButtonItem *customBarButtomItem = [[UIBarButtonItem alloc] initWithCustomView:parentView];
[parentView release];
self.navigationItem.rightBarButtonItem = customBarButtomItem;
[customBarButtomItem release];

我希望按钮看起来像普通的UIBarButtonItems,并且能够使用系统样式,如UIBarButtonSystemItemAdd、UIBarButtonSystemItemRefresh。我提供的链接可以实现这一点,但是由于它是嵌套在导航栏中的UIToolbar,所以你可以看到一个微弱的接缝。 - Cal
我之前尝试过,按钮看起来有所不同。但您可以拍摄一个截图图像,然后在按钮中使用它。 - Cal

2

可以在苹果网站上免费查看UICatalogue示例...他们使用UISegmentedControl在导航栏右侧按钮的位置显示了三个按钮...


抱歉,我不需要分段控件。我需要标准的按钮,如+(添加按钮)、编辑、刷新图标等。 - Cal
你可以添加一个自定义视图来放置你想要的按钮......在谷歌上搜索自定义视图代码......你将能够获取它。 - Rahul Vyas
有很多网络教程可以做到这一点。如果你已经尝试过,请发布你的代码。 - Rahul Vyas
我再次研究了一下,我认为这种方式实际上非常不错。唯一的问题是如果需要标准图标,则需要获取它们。如果您这样做,分段控制按钮看起来像普通的导航按钮。 - Cal
顺便说一句,我没有看到你提到的那个示例。我想我有一个不同版本的UICatalog项目。我确实在Interface Builder中看到了它。您可以将分段控件拖放到导航栏的左侧/右侧/中心。 - Cal

1

0
我想出了一个辅助函数,我在整个项目中都在使用它。基本上,它会检查栏上是否已经有按钮,然后添加新的按钮或将其与现有按钮合并。因此,您可以只调用该函数一次或多次:
+ (void)AddButtonToBar:(UIViewController *)controller withImage:(NSString *)imageName withAction:(SEL)action withFrame:(CGRect) frame{
    UIButton *newButton =[[UIButton alloc] init];
    [newButton setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    newButton.frame = frame;
    [newButton addTarget:controller action:action forControlEvents:UIControlEventTouchUpInside];

    if ([[controller.navigationItem rightBarButtonItems] count] == 0)
        [controller.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
    else {
        NSMutableArray *existingButtons = [[NSMutableArray alloc] initWithArray:[controller.navigationItem rightBarButtonItems]];
        [existingButtons addObject:[[UIBarButtonItem alloc] initWithCustomView:newButton]];
        [controller.navigationItem setRightBarButtonItems:(NSArray *)existingButtons];
    }
}

从视图控制器中调用它:

[Helper AddButtonToBar:self withImage:@"imageName.png" withAction:@selector(myAction) withFrame:CGRectMake(0, 0, 24, 24)];

0
在iOS 4.x中,clearColor似乎对UIToolbar没有影响,而覆盖其drawRect:方法可以解决这个问题。

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