2012-08-16 5 views
-1

iPad의 툴바에 UISegmentedControl을 추가하는 데 이상한 문제가 있습니다. 나는 다음과 같은 방법이있다 루트 컨트롤러와의 UINavigationController가 만든 :iPad의 툴바에 UISegmentedControl을 프로그래밍 방식으로 추가하는 방법

- (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>> 
+0

가 왜 IB를 사용하지 않는 결과를 얻을 수 있습니다? –

+0

좋은 질문이지만보기 컨트롤러를 생성 할 때 많은 사용자 지정 논리가 있습니다. 따라서 프로그래밍 방식으로 처리하는 것이 더 쉬워 보였습니다. – voromax

+0

downvoting에 대한 몇 가지 이유가 좋을 것입니다 ... – voromax

답변

0

해결 방법이 있습니다. UISegmentedControl을 툴바에 추가하려면 - (NSArray *)toolbarItems을 덮어 쓰는 것이 안전하지 않은 것 같습니다. 컨트롤러가로드 된 후 어딘가에 툴바를 구성하는 방법을 추가하는 것이 좋습니다.

- (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]; 
} 

이 방법을 사용하면

관련 문제