Multiple Barbutton in Navigation Bar not showing o

2019-08-06 05:26发布

问题:

I have added array of bar button to the navigation items using the property rightBarButtonItems,it work good for iOS5,when i tested in iOS6 only one bar button item is visible.

UIBarButtonItem *updateButton          = [[UIBarButtonItem alloc]
                                          initWithTitle:@"Update"
                                          style:UIBarButtonItemStylePlain
                                          target:self
                                          action:@selector(updateData)];

UIBarButtonItem *refreshButton          = [[UIBarButtonItem alloc]
                                          initWithTitle:@"Refresh"
                                          style:UIBarButtonItemStylePlain
                                          target:self
                                          action:@selector(refresh)];

NSArray *arrBtns = [[NSArray alloc]initWithObjects:updateButton,refreshButton, nil];

self.navigationItem.rightBarButtonItems=arrBtns;

Is there any new property for iOS6 to add the array of bar button to navigationitem.

Any help would be appreciated,Thanks a lot.

回答1:

Please use the segmentController on the rightBarButtonItems if you want to add multiButton on rightBarButtonItems of NavigationBar

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:@"Add",@"Delete",
                                             nil]];
    segmentedControl.frame = CGRectMake(0, 0, 80, 30);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl setWidth:35.0 forSegmentAtIndex:0];
    [segmentedControl setWidth:45.0 forSegmentAtIndex:1];

    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.momentary = YES;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];

    self.navigationItem.leftBarButtonItem = segmentBarItem;
    [segmentBarItem release];

Secondly add the second button on other side of the first bar Button.