ios-如何在导航栏的右侧添加多个UIBarButtonItems?

我想在UINavigationBar的右侧拥有多个UIBarButtonItem。如何实现呢?

下面显示了我正在尝试的示例-您会注意到右上角有多个按钮。

enter image description here

10个解决方案
131 votes

快速使用:

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]
}

编写如下的动作函数:

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

func didTapSearchButton(sender: AnyObject){
    ...
}
Dharmbir Singh answered 2019-11-08T07:02:19Z
8 votes
-(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];
}
Anoop answered 2019-11-08T07:02:40Z
5 votes

我认为以上都不起作用

试试这个

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)

您必须使用navigationItem.setRightBarButtonItems并格外小心。 navigationItem必须是一个视图控制器。

class testViewController:UIViewController {

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

}

}
Christos Chadjikyriacou answered 2019-11-08T07:03:20Z
5 votes

在Swift 3中,您可以使用:

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]
oscar castellon answered 2019-11-08T07:03:45Z
3 votes

只需添加以下代码:

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]

结果如下:

enter image description here

Mr.Javed Multani answered 2019-11-08T07:04:19Z
3 votes

使用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]

单击这些按钮的操作

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

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

输出屏幕截图(iPhone X)

enter image description here

Kavin Varnan answered 2019-11-08T07:05:00Z
0 votes

这可能对目标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;
}
B.Saravana Kumar answered 2019-11-08T07:05:24Z
0 votes

使用navigationItem的rightBarButtonItems,该数组采用UIBarButton的数组。

let share = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItems = [add, share]
user2672052 answered 2019-11-08T07:05:57Z
0 votes

我不知道以前是否无法使用Interface Builder解决方案,但是至少使用最新更新(Xcode 11.0),可以将两个UI Bar Buttons拖到rightBarButton空间中。 然后可以像往常一样将两个出口和动作拖到代码中。

由于OP在没有IB的情况下不要求解决方案,因此我认为这也可以作为答案。

bjrne answered 2019-11-08T07:06:32Z
-1 votes

对于具有新更新的Xamarin Ios。 它像是。

UIBarButtonItem Button2 = new UIBarButtonItem(UIImage.FromBundle("Image1"), UIBarButtonItemStyle.Plain,YourEventHandler);

UIBarButtonItem Button2 = new UIBarButtonItem(UIImage.FromBundle("Image1"), UIBarButtonItemStyle.Plain,YourEventHandler);

NavigationItem.SetRightBarButtonItems(new[]{ Button1, Button2 }, true);
Chandu Ranwala answered 2019-11-08T07:07:07Z
translate from https://stackoverflow.com:/questions/30341263/how-to-add-multiple-uibarbuttonitems-on-right-side-of-navigation-bar