2013-05-13 71 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 2013-05-13 21:36:12

+1

您將不得不稍後在視圖的生命週期中執行此項工作。子視圖等不是在調用init方法時設置的。在你的視圖控制器中從viewDidLoad調用工具欄上的setupLables方法。 – 2013-05-13 21:37:30

0

這不是一個uitoolbar。這是一個導航欄,所以請刪除導航欄並添加工具欄。試試這種類型。