I have some weird problem adding UISegmentedControl in toolbar on iPad. I have created a UINavigationController with the root controller which has the following methods:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UINavigationController *navigationController = [self navigationController];
navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
navigationController.toolbarHidden = NO;
}
- (NSArray *)toolbarItems
{
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
return [NSArray arrayWithObjects:[self segmentedControlItem], flexibleSpace, nil];
}
- (UISegmentedControl *)segmentedControl
{
if (_segmentedControl) {
return _segmentedControl;
}
NSArray *items = [NSArray arrayWithObjects:@"Segment 1", @"Segment 2", nil];
_segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
_segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
return _segmentedControl;
}
- (UIBarButtonItem *)segmentedControlItem
{
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedControl];
buttonItem.style = UIBarButtonItemStyleBordered;
return buttonItem;
}
But after the controller appears the segmentedControl is not visible on toolbar. How to fix it? I am already checked that the segmentedControl exists in toolbar items, it has size, it is not hidden, but nevertheless I cannot see it.
(lldb) po [[[[[self navigationController] toolbar] items] objectAtIndex:0] customView]
(id) $3 = 0x08e39a10 <UISegmentedControl: 0x8e39a10; frame = (7 8; 300 30); opaque = NO; layer = <CALayer: 0x8e63230>>
I have a workaround. It seems is not safe to overwrite the
- (NSArray *)toolbarItemsif you want to addUISegmentedControlin the toolbar. It is better to add some method to configure the toolbar somewhere after the controller did load.This approach allows you to get the result