iOS - UINavigationController adding multiple right items?

前端 未结 6 528
终归单人心
终归单人心 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题