How to add multiple UIBarButtonItems on right side

2019-03-08 02:02发布

问题:

I would like to have more than a single UIBarButtonItem on the right side of my UINavigationBar. How can I achieve this?

An example of what I am trying are shown below - you can notice that the top right has more than one button.

回答1:

Use this in swift:

override func viewDidLoad() {
  super.viewDidLoad()

  let editImage    = UIImage(named: "plus")!
  let searchImage  = UIImage(named: "search")!

  let editButton   = UIBarButtonItem(image: editImage,  style: .Plain, target: self, action: "didTapEditButton:")
  let searchButton = UIBarButtonItem(image: searchImage,  style: .Plain, target: self, action: "didTapSearchButton:")

  navigationItem.rightBarButtonItems = [editButton, searchButton]
}

Write the action functions like this:

func didTapEditButton(sender: AnyObject){
    ...
}

func didTapSearchButton(sender: AnyObject){
    ...
}


回答2:

-(void)viewDidLoad{

    UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Button_1" style:UIBarButtonItemStylePlain target:self action:@selector(button_1:)];

    UIBarButtonItem *anotherButton2 = [[UIBarButtonItem alloc] initWithTitle:@"Button_" style:UIBarButtonItemStylePlain target:self action:@selector(button_2:)];

    self.navigationItem.rightBarButtonItems=@[anotherButton1,anotherButton2];
}


回答3:

I think nothing of the above is going to work

Try this

var burgerItem = UIBarButtonItem(image: UIImage(named:"categories"), style: .Plain, target: self, action: "categories")
        var weatherItem = UIBarButtonItem(title: "Weather", style: .Plain, target: self, action: "weather")


        burgerItem.tintColor = UIColor.whiteColor()
        weatherItem.tintColor = UIColor.whiteColor()

        navigationItem.setRightBarButtonItems([burgerItem,weatherItem], animated: true)

You have to use navigationItem.setRightBarButtonItems and be carefull. navigationItem has to be of a view controller.

class testViewController:UIViewController {

ovverride func viewDidLoad() {
    self.navigationItem.setRightBarButtonItems(...

}

}


回答4:

In Swift 3 you can use:

let editImage   = UIImage(named: "plus")!
let searchImage = UIImage(named: "search")!

let editButton   = UIBarButtonItem(image: editImage,  style: .plain, target: self, action: #selector(didTapEditButton))
let searchButton = UIBarButtonItem(image: searchImage,  style: .plain, target: self, action: #selector(didTapSearchButton))

navigationItem.rightBarButtonItems = [editButton, searchButton]


回答5:

Simply add this code:

self.navigationItem.leftBarButtonItem = nil

let button = UIButton(type: .custom)
button.setImage(UIImage (named: "ChatTab"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)

let button2 = UIButton(type: .custom)
button2.setImage(UIImage (named: "ActivityTab"), for: .normal)
button2.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
//button.addTarget(target, action: nil, for: .touchUpInside)

let barButtonItem2 = UIBarButtonItem(customView: button2)
self.navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]

This is the result:



回答6:

This is may be help for you in objective-c,

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"Title";
        UIBarButtonItem *logOutButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(ButtonClickedAtIndex1:)];
        [logOutButton setTintColor:[UIColor blackColor]];
        logOutButton.tag = 1;

        UIBarButtonItem *syncBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Sync" style:UIBarButtonItemStylePlain target:self action:@selector(ButtonClickedAtIndex1:)];
        [syncBarButtonItem setTintColor:[UIColor blackColor]];
        syncBarButtonItem.tag = 2;

        self.navigationItem.leftBarButtonItem = logOutButton;
        self.navigationItem.rightBarButtonItem = syncBarButtonItem;

       float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

       if (systemVersion >= 7.0) {
           self.edgesForExtendedLayout = UIRectEdgeNone;
           self.navigationController.navigationBar.translucent = NO;
       }
    }
    return self;
}


回答7:

Use navigationItem's rightBarButtonItems, which takes an array of UIBarButton. Appledoc

let share = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItems = [add, share]


回答8:

With Swift 4

let editImage       = UIImage(named: "toolbar_edit")!
let favoriteImage   = UIImage(named: "toolbar_fav_solid")!

let editButton      = UIBarButtonItem(image: editImage,  style: .plain, target: self, action: #selector(didTapEditButton))
let favoriteButton  = UIBarButtonItem(image: favoriteImage,  style: .plain, target: self, action: #selector(didTapFavoriteButton))

navigationItem.rightBarButtonItems = [editButton, favoriteButton]

Click action for those buttons

@objc func didTapEditButton(sender: AnyObject) {
    print("edit")
}

@objc func didTapFavoriteButton(sender: AnyObject) {
    print("favorite")
}

Output screenshot (iPhone X)