How to add right button in the navigation bar?

前端 未结 6 627
傲寒
傲寒 2021-02-05 16:40

I have a question to add a right button in navigation bar..

I have two views: View A and View B

I add a navigation bar to view A, after I used self.navigat

相关标签:
6条回答
  • 2021-02-05 17:09

    Add below code in viewDidLoad Method

    UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] 
                                   initWithTitle:@"Flip"                                            
                                   style:UIBarButtonItemStyleBordered 
                                   target:self 
                                   action:@selector(flipView:)];
    self.navigationItem.rightBarButtonItem = flipButton;
    

    This adds a button to the right hand side with the title Flip, which calls the method:

    -(IBAction)flipView
    
    0 讨论(0)
  • 2021-02-05 17:10

    You can add the following code to your B controller's viewDidLoad method.

    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                                                        style:UIBarButtonItemStyleDone 
                                                                        target:self 
                                                                        action:@selector(rightButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    0 讨论(0)
  • 2021-02-05 17:17

    The Swift version of Vahan Babayan's answer, as you seem to use this language, is:

    let rightButtonItem = UIBarButtonItem.init(
          title: "Title", 
          style: .Done, 
          target: self, 
          action: "rightButtonAction:"
    )
    
    self.navigationItem.rightBarButtonItem = rightButtonItem
    

    The following method will be called on self:

    func rightButtonAction(sender: UIBarButtonItem)
    

    Note that all this can be set graphically using a Storyboard, by dragging a Bar Button Item to your Navigation Item and right-clicking to set a target-action.


    A small update since Swift 3 and 4 are out: the compiler can now check selector names, preventing typos when setting up target-action programatically. So one should really use:

    let rightButtonItem = UIBarButtonItem.init(
          title: "Title", 
          style: .Done, 
          target: self, 
          action: #selector(rightButtonAction(sender:))
    )
    
    0 讨论(0)
  • 2021-02-05 17:19

    Swift code to add a navigation bar button item:

    Method 1 (When you wanna use bar button system item)

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    

    Method 2 (When you wanna give your own title to button)

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    

    From iOS 5 onwards, it allows you to add more than 1 button on either side of a navigation bar. Something like this:

    let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    let display = UIBarButtonItem(title: "Display", style: .plain, target: self, action: #selector(playTapped))
    
    navigationItem.rightBarButtonItems = [add, display]
    
    0 讨论(0)
  • 2021-02-05 17:21
    - (void)viewDidLoad {
    
        [super viewDidLoad];
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Right Button" style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnClick)];
        self.navigationItem.rightBarButtonItem = rightBtn;
    }
    
    -(void)rightBtnClick{
        // code for right Button
    
    }
    
    0 讨论(0)
  • 2021-02-05 17:22

    Try this code for Swift UI :

     .navigationBarItems(
       leading:
         HStack {
           Button("About") {
             print("About tapped!")
           }
           Button("Call") {
             print("Call tapped!")
           }
         },
                                        
       trailing:
         HStack {
           Button("Settings") {
             print("Settings tapped!")
           }
           Button("Contacts") {
             print("Contacts tapped!")
           }           
         }
      )
    
    0 讨论(0)
提交回复
热议问题