我在 iPad 的工具栏中添加 UISegmentedControl 时遇到了一些奇怪的问题。我用根控制器创建了一个 UINavigationController,它具有以下方法:
- (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;
}
但是在控制器出现后,segmentedControl 在工具栏上不可见。如何解决?我已经检查了segmentedControl 是否存在于工具栏项目中,它有大小,它没有隐藏,但是我看不到它。
(lldb) po [[[[[self navigationController] toolbar] items] objectAtIndex:0] customView]
(id) $3 = 0x08e39a10 <UISegmentedControl: 0x8e39a10; frame = (7 8; 300 30); opaque = NO; layer = <CALayer: 0x8e63230>>