2013-05-13 55 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。这是一个导航栏,所以请删除导航栏并添加工具栏。试试这种类型。