2013-05-13 5 views
0

서브 클래 싱 된 UIToolBar에 레이블을 설정하려고합니다. 먼저 라벨을 생성 한 다음 UIBarButtonItem으로 변환하고 툴바로 설정하지만 툴바는 그냥 비어 있습니다. 여기레이블이 UIToolBar에 표시되지 않습니다.

내 서브 클래 싱 된 도구 모음

NSMutableArray *items = [[NSMutableArray alloc] init]; 

// prep the total label 
_totalLabel = [[UILabel alloc] init]; 
_totalLabel.font = [_totalLabel.font fontWithSize:15]; 
_totalLabel.textColor = [UIColor whiteColor]; 
_totalLabel.text = NSLocalizedString(@"TOTAL", nil); 
_totalLabel.frame = CGRectMake(0, 0, 100, 44); 
_totalLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; 
_totalLabel.layer.borderWidth = 2; 
_totalLabel.layer.borderColor = [UIColor greenColor].CGColor; 
_totalLabel.backgroundColor = [UIColor clearColor]; 

UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
[items addObject:spacer2]; 

UIBarButtonItem *total = [[UIBarButtonItem alloc] initWithCustomView:_totalLabel]; 
[items addObject:total]; 

[self setItems:items]; 

내 init 메소드에 무슨 잘못 어떤 생각이 내 코드입니까?

답변

0

이 코드를 어디에서 불러들입니까? initWithFrame: 또는 initWithCoder: 인 경우 작동하지 않습니다.

이 시도 :

- (void)layoutSubviews { 

    [super layoutSubviews]; 

    if(self.items.count == 0) { 
     //Your code here 
    } 
} 

을이 작업을 얻을 것이다. 질문은 당신이 layoutSubviews에서 이것을해야 하는가? 아마도 그렇지 않습니다. UIToolbar에 설정 방법을 넣고 나중에 응용 프로그램의 호출 지점에서 호출 할 수도 있습니다.

+0

일종. 내 init 메서드는 [self setLabels]를 호출하고 위의 코드는 해당 메서드에 있습니다. – JMD

+1

나중에 뷰의 수명주기에서이 작업을 수행해야합니다. 하위 메서드 등은 init 메서드가 호출 될 때 설정되지 않습니다. 보기 컨트롤러에서 viewDidLoad :의 툴바에있는 setupLables 메소드를 호출하십시오. –

0

이것은 도구 모음이 아닙니다. 이것은 탐색 모음이므로 탐색 표시 줄을 제거하고 도구 모음을 추가하십시오. 이 유형을 시도하십시오.

관련 문제