-1

我在 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>>
4

1 回答 1

0

我有一个解决方法。- (NSArray *)toolbarItems如果要在工具栏中添加,覆盖似乎是不安全UISegmentedControl的。最好在控制器加载后添加一些方法来配置工具栏。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configureToolbar];
}

- (void)configureToolbar
{
    UINavigationController *navigationController = [self navigationController];
    navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
    navigationController.toolbarHidden = NO;
    // Add toolbar items here
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
    self.toolbarItems = [NSArray arrayWithObjects:[self segmentedControlItem], flexibleSpace, nil];
}

这种方法可以让你得到结果

于 2012-08-16T13:04:26.840 回答