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

后端 未结 6 557
天命终不由人
天命终不由人 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 07:03

    EDIT

    At the time I answered this question, Xcode was not offering the possibility of linking added buttons in the storyboard. The trick presented permitted to still have the segues designed in the storyboard.

    With more recent versions of Xcode, for sure the solution introduced by @ecotax and later the more detailed answer of @ShimanskiArtem are the ones to be used.


    I had the same problem as you and I found the following trick

    Suppose you have a navigationController in which you would like to have multiple buttons. Since iOS 5 you can assign an array. The problem is that you lose all the benefits of using the storyboard as it will be done programmatically.

    I used the following trick. Usually when you want multiple button on the navigation bar you don't want a toolbar.

    In the current view (not in the navigation controller) where you want the buttons to appear, show the toolbar by changing

    bottomBar = inferred to bottomBar = toolbar.

    enter image description here

    A toolbar will appear at the bottom. Add UIBarButtons to this bar. Link them to other view controllers using segues, etc ...

    in your .h file create an outlet for each button

    @property (strong, nonatomic) IBOutlet UIBarButtonItem *Button1;
    @property (strong, nonatomic) IBOutlet UIBarButtonItem *Button2;
    @property (strong, nonatomic) IBOutlet UIBarButtonItem *Button3;
    

    Then in your viewDidLoad() link the buttons to the navigation bar and hide the toolbar. Add them in the reverse order of the order you want to see them

    self.navigationItem.rightBarButtonItems =
        [NSArray arrayWithObjects:self.Button3, self.Button2, self.Button1, nil];
    
    self.navigationController.toolbarHidden = YES;
    

    And voilà you have multiple buttons in your navigation bar

    enter image description here

    enter image description here

    and the result in the simulator

    enter image description here

    enter image description here

提交回复
热议问题