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

时间:2009-09-12 06:58:34

标签: iphone uinavigationbar uibarbuttonitem

我尝试过这种方法/ hack: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/

问题是这会留下一个微弱的缝隙。我尝试将嵌套工具栏的背景图像设置为我捕获的应该是什么的图像。那没用。图像未应用。我也试过使用嵌套的UINavigationBar,但似乎没有用。

我已在多个iPhone应用中看到过这种情况。有谁知道怎么做?

[编辑]我希望按钮看起来像普通的UIBarButtonItems,并且能够使用系统样式,如UIBarButtonSystemItemAdd,UIBarButtonSystemItemRefresh。我提供的链接执行此操作,除了您可以看到一个微弱的接缝,因为它是嵌套在导航栏中的UIToolbar ..

请不要提及违反人机界面指南。 (我们知道)。

我感谢你贡献你的黑客......这是唯一的方法!

8 个答案:

答案 0 :(得分: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;

答案 1 :(得分:18)

我发布了code to add two buttons to the right of the navigationBar。您可以设置barStyle = -1,而不是子类化UIToolbar

答案 2 :(得分:6)

要删除UIToolbar的背景('seam'),请创建UIToolbar的子类并覆盖(void)drawRect:(CGRect)rect方法。将其留空,您的UIToolbar将不再具有背景。

在我自己的项目中使用它并且工作得很好。在以下评论中找到:http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

答案 3 :(得分: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];

答案 4 :(得分:2)

答案 5 :(得分:1)

我无法发表评论,但除了@iworkinprogress,我还必须将UIToolbar背景颜色设置为清除:

    [toolbar setBackgroundColor:[UIColor clearColor]];

http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html的评论也发现了这一点。

答案 6 :(得分:0)

在iOS 4.x中,clearColor似乎对UIToolbar没有影响,而覆盖了它的drawRect:did。

答案 7 :(得分: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)];