Can't assign multiple Buttons to UINavigationItem when using Storyboard with iOS 5

后端 未结 6 564
天命终不由人
天命终不由人 2020-11-28 06:15

I\'m a iOS developer with a lot experience in developing the UI by code.

I\'m now testing the Storyboard functionality, because I testing to switch to \"design\" the

6条回答
  •  Happy的楠姐
    2020-11-28 07:02

    I found an easy solution.

    1) Add the folowing category:

    @interface UINavigationItem(MultipleButtonsAddition)
    @property (nonatomic, strong) IBOutletCollection(UIBarButtonItem) NSArray* rightBarButtonItemsCollection;
    @property (nonatomic, strong) IBOutletCollection(UIBarButtonItem) NSArray* leftBarButtonItemsCollection;
    @end
    
    @implementation UINavigationItem(MultipleButtonsAddition)
    
    - (void) setRightBarButtonItemsCollection:(NSArray *)rightBarButtonItemsCollection {
        self.rightBarButtonItems = [rightBarButtonItemsCollection sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
    }
    
    - (void) setLeftBarButtonItemsCollection:(NSArray *)leftBarButtonItemsCollection {
        self.leftBarButtonItems = [leftBarButtonItemsCollection sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"tag" ascending:YES]]];
    }
    
    - (NSArray*) rightBarButtonItemsCollection {
        return self.rightBarButtonItems;
    }
    
    - (NSArray*) leftBarButtonItemsCollection {
        return self.leftBarButtonItems;
    }
    
    @end
    

    2) Add your items to view controller (items will be sorted ascending by tag)

    enter image description here

    3) Connect your items with leftBarButtonItemsCollection or rightBarButtonItemsCollection outlet collection of UINavigationItem

    enter image description here

提交回复
热议问题