0

我正在尝试将标签设置为子类 UIToolBar。我首先创建标签,然后将其转换为 UIBarButtonItem 并将它们设置为工具栏,但工具栏只是显示为空白。

这是我的子类工具栏的 init 方法中的代码

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

知道有什么问题吗?

4

2 回答 2

0

您从哪里调用此代码?如果你有它,initWithFrame:否则initWithCoder:它将无法工作。

尝试这个:

- (void)layoutSubviews {

    [super layoutSubviews];

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

这将使它工作。问题是你应该这样做layoutSubviews吗?可能不是。您还可以在您的应用程序中放置一个设置方法,UIToolbar然后在稍后的应用程序中调用它。

于 2013-05-13T21:31:18.033 回答
0

这不是 uitoolbar。这是一个导航栏,所以请删除导航栏并添加工具栏。试试这种类型。

于 2013-05-18T09:24:09.013 回答