开发者

Adding toolbar to navigation controller - color mismatch

I used following code to add multiple buttons to the navigation controller

UIToolbar* toolbar = [[UIToolbar alloc]
                      initWithFrame:CGRectMake(0, 0, 100, 44)];
toolbar.tintColor = [UIColor cle开发者_如何学PythonarColor];
[toolbar setBarStyle: UIBarStyleBlackTranslucent];

...

The problem is the background of the toolbar does not match 100% to the navigation bar. There is a small line showing at the top of the toolbar. The color is almost the same but if you look carefully you can see the rectangle …

I do following in the delegate to set the background of the navigation bar

self.navigationController.navigationBar.tintColor =  [UIColor colorWithRed:.0 green:.0 blue:.0 alpha:.4];

any ideas how to get the background color to match the navigation bar color?

Adding toolbar to navigation controller - color mismatch


The best way to make this work is to make the toolbar fully transparent. One way to do this is to subclass UIToolbar and override drawRect: to do nothing.

Here's my implementation of a UITransparentToolbar (note this assumes the toolbar will be created via a xib.):

@implementation UITransparentToolbar

- (id)initWithCoder:(NSCoder *)decoder
{
    if ( self = [super initWithCoder:decoder] )
    {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
        self.translucent = YES;

    }
    return self;
}

- (void)drawRect:(CGRect)rect 
{
}

- (void)dealloc 
{
    [super dealloc];
}

@end


Looks like iOS 5 or later supports this without a trick according to this answer:

https://stackoverflow.com/a/9109910/1179521

And it worked for me!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜