0

我正在尝试添加一个 UIToolbar,以在 NavigationBar 中显示用户业力。我遇到的问题是颜色不完全相等,正如您在照片中看到的那样。

在此处输入图像描述

那么,任何想法我怎么能解决这个问题?这是代码:

- (void)viewDidLoad
{

[super viewDidLoad];
UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 45.00f)];

UILabel *karma = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 0.0f, 40.0f, 44.00f)];
karma.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"karma"];
karma.font = [UIFont fontWithName:@"Helvetica" size:(14.0)];
karma.textColor = [UIColor colorWithRed:(255.0) green:(255.0) blue:(255.0) alpha:1];
karma.backgroundColor = [UIColor colorWithRed:(64/255.0) green:(64/255.0) blue:(64/255.0) alpha:0];

UIImage *image = [UIImage imageNamed:@"ic_people.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0.0f, 7.5f, 30.0f, 30.00f);
// Add Pin button.

[tools setBarStyle: UIBarStyleDefault];
[tools addSubview:karma];
[tools addSubview:imageView];

// Add toolbar to nav bar.
UIBarButtonItem *karmaNav = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = karmaNav;
...

谢谢你。

4

1 回答 1

1

为什么不将你的业力标签和图像嵌入 UIView 而不是 UIToolbar 中。然后,您可以将该 UIView 的背景颜色设置为 clearColor。

像这样的东西:

UIView *tools = [[UIView alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 45.00f)];
tools.backgroundColor = [UIColor clearColor];

UILabel *karma = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 0.0f, 40.0f, 44.00f)];
karma.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"karma"];
karma.font = [UIFont fontWithName:@"Helvetica" size:(14.0)];
karma.textColor = [UIColor colorWithRed:(255.0) green:(255.0) blue:(255.0) alpha:1];
karma.backgroundColor = [UIColor colorWithRed:(64/255.0) green:(64/255.0) blue:(64/255.0) alpha:0];

UIImage *image = [UIImage imageNamed:@"ic_people.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0.0f, 7.5f, 30.0f, 30.00f);

[tools addSubview:karma];
[tools addSubview:imageView];

UIBarButtonItem *karmaNav = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = karmaNav;
于 2013-08-09T16:16:01.550 回答