我想制作一个包含图像和文本的自定义 UIBarButtonItem,如下所示:
我尝试继承 UIBarButtonItem 并覆盖此方法:
- (UIView *)customView
{
if (!self.storedView) {
UIView *temp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:self.image];
tempImageView.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 100, 44)];
tempLabel.text = @"text";
[temp addSubview:tempImageView];
[temp addSubview:tempLabel];
self.storedView = temp;
}
return self.storedView;
}
我像这样使用它:
UIBarButtonItem *left = [[LeftItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(settingsPressed)];
left.title = @"Settings";
left.image = [UIImage imageNamed:@"settings.png"];
self.navigationItem.leftBarButtonItem = left;
但是使用这个我只得到图像,而不是标签。我究竟做错了什么?