简体   繁体   中英

How do I show exactly the same color in UIToolbar and NavigationBar?

I'm trying to add a UIToolbar that shows the user karma in a NavigationBar. The problem I have is that the colors are not exactly equals, as you can see at the photo.

在此处输入图片说明

So, any idea How could I resolve this? Here is the code:

- (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;
...

Thank you.

Why not embed your karma label and image inside of a UIView instead of a UIToolbar. Then, you'd be able to set the background color of that UIView to clearColor.

Something like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM