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

StackOverflow https://stackoverflow.com/questions/1414510

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

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

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

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

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

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

有帮助吗?

解决方案

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

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

其他提示

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;

我发布了代码,以便在navigationBar右侧添加两个按钮。您可以设置 barStyle = -1 ,而不是子类化 UIToolbar

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];

请参阅苹果网站上免费提供的uicatalogue示例...他们使用uisegmented控件在导航栏上显示三个按钮代替右侧栏按钮...

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

    [toolbar setBackgroundColor:[UIColor clearColor]];

http: //osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

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

我想出了一个帮助函数,我正在我的项目中使用它。基本上它会检查条上是否有按钮,并添加新按钮或将其与现有按钮合并。所以你可以只调用一次或多次函数:

+ (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)];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top