iOS - UINavigationController adding multiple right items?

前端 未结 6 522
终归单人心
终归单人心 2021-02-01 15:07

I have a UINavigationController. I\'m trying to add multiple buttons on the right side of my navigationBar. How can I achieve this? What kind of button does it take

相关标签:
6条回答
  • 2021-02-01 15:35

    As of iOS5 you can assign an array of bar button items to the navigation item's rightBarButtonItems (note the plural) property.

    0 讨论(0)
  • 2021-02-01 15:35

    there's actually even a bit ore hacky, but at the same time more cleaner way of doing this stuff: just define a category on UINavigationItem, like:

    UINavigationItem+Toolbars.h:

    @interface UINavigationItem (Toolbars)
    
    @property (nonatomic, retain) IBOutlet UIToolbar * rightToolBar;
    @property (nonatomic, retain) IBOutlet UIToolbar * leftToolBar;
    
    - (void)setRightToolBar:(UIToolbar *)_rightToolBar;
    - (UIToolbar *)rightToolBar;
    - (void)setLeftToolBar:(UIToolbar *)_leftToolBar;
    - (UIToolbar *)leftToolBar;
    
    @end
    

    UINavigationItem+Toolbars.m:

    #import "UINavigationItem+Toolbars.h"
    
    @implementation UINavigationItem (Toolbars)
    
    - (void)setRightToolBar:(UIToolbar *)_rightToolBar {
        self.rightBarButtonItems = _rightToolBar.items;
    }
    
    - (UIToolbar *)rightToolBar {
        return nil;
    }
    
    - (void)setLeftToolBar:(UIToolbar *)_leftToolBar {
        self.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:_leftToolBar] autorelease];
    }
    
    - (UIToolbar *)leftToolBar {
        return nil;
    }
    
    @end
    

    After doing that, just assign an outlet in IB settings a toolbar (just create one) and enjoy buttons appearing on the navigation item.

    0 讨论(0)
  • 2021-02-01 15:39

    I am sure I read in the developer reference that additional buttons in the navigation bar is frowned upon. I cannot find that passage now. I have not done it myself, but found this link that seems to outline exactly what you need to do: (http://www.mattdipasquale.com/blog/2010/11/02/how-to-add-multiple-uibarbuttonitems-to-uinavigationbar/)

    Have you considered using the toolbar property of the navigation controller?

    0 讨论(0)
  • 2021-02-01 15:41

    I used JRTurtons answer in Xcode 4.5, iOS 6 and implemented it like this and it works:

    // Two buttons at the right side of nav bar
    UIBarButtonItem *addAttachButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAttachmentClicked:)];
    UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(@"Send") style:UIBarButtonItemStyleBordered target:self action:@selector(sendClicked:)];
    self.navigationItem.rightBarButtonItems = @[addAttachButton,sendButton];
    

    However, I should mention, that UIBarButtonSystemItemPageCurl doesn't work like that.

    0 讨论(0)
  • 2021-02-01 15:43

    adding any design to the navigation controller in XCode is easy.

    add a UIView to your scene add the buttons you need to the UIView then drag and drop the UIView to the right space in the navigationController

    0 讨论(0)
  • 2021-02-01 15:44

    In Xcode 7.1 (perhaps even earlier), you can add multiple items to the right or left side of a UINavigationBar just by dragging them in. If you drag to just the right spot, you get a little vertical bar that indicates where that item will be inserted.

    0 讨论(0)
提交回复
热议问题