24

我有一个带有左右按钮的导航栏,我需要在右侧按钮旁边放另一个按钮。有谁知道我该怎么做?这里有一些代码可以提供帮助:

- (id)init {
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {

        _pinArray = [[NSArray alloc]init];
        _pinArray = [Data singleton].annotations;

        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"Map"
                                                                 style:UIBarButtonItemStylePlain
                                                            target:self
                                                            action:@selector(goToMap:)];
        self.navigationItem.rightBarButtonItem = right;

        UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"Menu"
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:@selector(goToMenu:)];
        self.navigationItem.leftBarButtonItem = left;
        self.navigationItem.title = @"My Homes";
    }
    return self;
}
4

3 回答 3

53

这很容易:)

https://developer.apple.com/documentation/uikit/uinavigationitem/1624956-rightbarbuttonitems

navigationItem.rightBarButtonItems = [rightA, rightB] // @[rightA, rightB] for ObjC
于 2013-01-15T01:50:26.213 回答
7

而不是使用self.navigationItem.rightBarButtonItem,使用

self.navigationItem.rightBarButtonItems //note the plural

这允许您设置一组按钮而不是单个按钮。

有关详细信息,请参阅UINavigationItem 类参考

于 2013-01-15T01:51:17.697 回答
2
  let RightBarButton = UIButton()
        RightBarButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
        RightBarButton.frame = CGRectMake(30,0,30,30)
        RightBarButton.setImage(UIImage(named: "search-icon.png"), forState: .Normal)
        RightBarButton.addTarget(self, action: #selector(BaseViewController.OpenQuickLink), forControlEvents: .TouchUpInside)

        let RightBarButton2 = UIButton()
        RightBarButton2.setTitleColor(UIColor.blueColor(), forState: .Normal)
        RightBarButton2.frame = CGRectMake(0,0,30,30)
        RightBarButton2.setImage(UIImage(named: "share-icon.png"), forState: .Normal)
        RightBarButton2.addTarget(self, action: #selector(BaseViewController.Opensharelink), forControlEvents: .TouchUpInside)
        let barButtonItem1 = UIBarButtonItem(customView: RightBarButton2)

        let barButtonItem = UIBarButtonItem(customView: RightBarButton)


navigationItem.rightBarButtonItems = [barButtonItem1, barButtonItem2]
于 2016-07-12T11:22:29.123 回答