How to add a button to UINavigationBar?

后端 未结 7 1736
暖寄归人
暖寄归人 2020-12-02 06:19

How to add a button to UINavigationBar programmatically?

相关标签:
7条回答
  • 2020-12-02 06:43

    swift 3

        let cancelBarButton = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(cancelPressed(_:)))
        cancelBarButton.setTitleTextAttributes( [NSFontAttributeName : UIFont.cancelBarButtonFont(),
                                                              NSForegroundColorAttributeName : UIColor.white], for: .normal)
        self.navigationItem.leftBarButtonItem = cancelBarButton
    
    
        func cancelPressed(_ sender: UIBarButtonItem ) {
            self.dismiss(animated: true, completion: nil)
        }
    
    0 讨论(0)
  • 2020-12-02 06:52

    The answers above are good, but I'd like to flesh them out with a few more tips:

    If you want to modify the title of the back button (the arrow-y looking one at the left of the navigation bar) you MUST do it in the PREVIOUS view controller, not the one for which it will display. It's like saying "hey, if you ever push another view controller on top of this one, call the back button "Back" (or whatever) instead of the default."

    If you want to hide the back button during a special state, such as while a UIPickerView is displayed, use self.navigationItem.hidesBackButton = YES; and remember to set it back when you leave the special state.

    If you want to display one of the special symbolic buttons, use the form initWithBarButtonSystemItem:target:action with a value like UIBarButtonSystemItemAdd

    Remember, the meaning of that symbol is up to you, but be careful of the Human Interface Guidelines. Using UIBarButtonSystemItemAdd to mean deleting an item will probably get your application rejected.

    0 讨论(0)
  • 2020-12-02 06:52

    In Swift 2, you would do:

    let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
    self.navigationItem.rightBarButtonItem = rightButton
    

    (Not a major change) In Swift 4/5, it will be:

    let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: nil, action: nil)
    self.navigationItem.rightBarButtonItem = rightButton
    
    0 讨论(0)
  • 2020-12-02 06:53

    Sample code to set the rightbutton on a NavigationBar.

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
        style:UIBarButtonItemStyleDone target:nil action:nil];
    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
    item.rightBarButtonItem = rightButton;
    item.hidesBackButton = YES;
    [bar pushNavigationItem:item animated:NO];
    

    But normally you would have a NavigationController, enabling you to write:

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
        style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    0 讨论(0)
  • 2020-12-02 06:55

    The example below will display a button with a title "Contact" on the navigation bar on the right. Its action calls a method named "contact" from the viewcontroller. Without this line the right button is not visible.

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Contact"
                                                                              style:UIBarButtonItemStylePlain target:self action:@selector(contact:)];;
    

    enter image description here

    0 讨论(0)
  • 2020-12-02 06:57

    Adding custom button to navigation bar ( with image for buttonItem and specifying action method (void)openView{} and).

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 32, 32);
    [button setImage:[UIImage imageNamed:@"settings_b.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
    [barButton setCustomView:button];
    self.navigationItem.rightBarButtonItem=barButton;
    
    [button release];
    [barButton release];
    
    0 讨论(0)
提交回复
热议问题