带有多个按钮的导航栏

时间:2013-01-15 01:45:01

标签: ios objective-c uinavigationcontroller uibarbuttonitem

我有一个带左右按钮的导航栏,我需要在右键旁边放另一个按钮。有谁知道我怎么能这样做?以下是一些有用的代码:

- (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;
}

3 个答案:

答案 0 :(得分:47)

这很容易:)

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

navigationItem.rightBarButtonItems = [rightA, rightB] // @[rightA, rightB] for ObjC

答案 1 :(得分:7)

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

self.navigationItem.rightBarButtonItems //note the plural

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

有关详细信息,请参阅UINavigationItem class reference

答案 2 :(得分:1)

  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]