我正在尝试将标签设置为子类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方法的任何想法什么是错?
有点儿。我的init方法调用[self setLabels],上面的代码在该方法中 – JMD 2013-05-13 21:36:12
您将不得不稍后在视图的生命周期中执行此项工作。子视图等不是在调用init方法时设置的。在你的视图控制器中从viewDidLoad调用工具栏上的setupLables方法。 – 2013-05-13 21:37:30