More than 1 rightBarButtonItem on navigation bar

前端 未结 9 993
清歌不尽
清歌不尽 2021-01-30 02:06

I would like to have two rightBarButtonItems on navigation bar. One for Edit and the other for Add.

Obviously I can\'t make it using Interface Builder.

Does anyb

9条回答
  •  暖寄归人
    2021-01-30 02:39

    Heres an example of how you can add two buttons as the rightbarbutton. The code below creates a segmentedcontrol containing up and down arrow buttons, which is then added as a customview for navigation's rightbarbutton:

    UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
        [segmentedControl setMomentary:YES];
        [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-up.png"] atIndex:0 animated:NO];
        [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-down.png"] atIndex:1 animated:NO];
        segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    
        UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
        self.navigationItem.rightBarButtonItem = segmentBarItem;
    

提交回复
热议问题