How to add multiple UIBarButtonItems on right side

2019-03-08 01:28发布

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.

enter image description here

8条回答
地球回转人心会变
2楼-- · 2019-03-08 02:05
-(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];
}
查看更多
Juvenile、少年°
3楼-- · 2019-03-08 02:06

This is may be help for you in ,

- (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;
}
查看更多
We Are One
4楼-- · 2019-03-08 02:14

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(...

}

}
查看更多
虎瘦雄心在
5楼-- · 2019-03-08 02:15

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:

enter image description here

查看更多
可以哭但决不认输i
6楼-- · 2019-03-08 02:16

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]
查看更多
虎瘦雄心在
7楼-- · 2019-03-08 02:19

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){
    ...
}
查看更多
登录 后发表回答