【问题标题】:How do you add more than one UIBarButton on UINavigationItem.rightBarButtonItem (or leftBarButtonItem)?如何在 NavigationItem.rightBarButtonItem(或 leftBarButtonItem)上添加多个 UIBarButton?
【发布时间】:2009-09-12 06:58:33
【问题描述】:

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

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

我已经在几个 iPhone 应用程序中看到了这一点。有人知道怎么做吗?

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

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

感谢您贡献自己的技巧...这是做到这一点的唯一方法!

【问题讨论】:

  • 你提到的链接失效了:-(

标签: iphone uinavigationbar uibarbuttonitem


【解决方案1】:

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;

【讨论】:

    【解决方案2】:

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

    【讨论】:

    【解决方案3】:

    要摆脱 UIToolbar 的背景(“接缝”),请创建 UIToolbar 的子类并覆盖 (void)drawRect:(CGRect)rect 方法。将其留空,您的 UIToolbar 将不再有背景。

    刚刚在我自己的项目中使用它并且效果很好。在以下的 cmets 中找到这个:http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html

    【讨论】:

    • 仅供参考,iOS 5.0 支持多个导航栏按钮。请参阅我在下面发布的答案。但是,如果您需要为旧版本的操作系统进行部署,那么您仍然需要使用此答案中描述的技术。
    【解决方案4】:
    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。
    • 我之前试过这个,按钮看起来不一样。但是您可以截取您想要的外观并在按钮中使用它。
    【解决方案5】:

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

    【讨论】:

    • 对不起。我不是在寻找段控制。我想要标准按钮,如 +(添加按钮)、编辑、刷新图标等。
    • 您可以使用所需的按钮添加自定义视图...在谷歌搜索自定义视图代码..您将能够得到它
    • 网上有很多教程可以做到这一点。如果你试过发布你的代码
    • 我又看了一遍,觉得这种方式其实还不错。唯一的问题是在需要时获取标准图标。如果你这样做,段控制按钮看起来就像普通的导航按钮。
    • 顺便说一句,我没有看到您所说的示例。我猜我有一个不同版本的 UICatalog 项目。我确实在 Interface Builder 中看到了它。您可以将分段控件拖放到导航栏左/右/中心。
    【解决方案6】:

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

        [toolbar setBackgroundColor:[UIColor clearColor]];
    

    这也在http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html的cmets中找到。

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

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

        + (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)];
        

        【讨论】:

          猜你喜欢
          • 2012-03-29
          • 1970-01-01
          • 2014-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-15
          相关资源
          最近更新 更多