UINavigationBar Background Color

iOS 5 support appearance to change the UINavigationBar Design. However, we can’t use UINavigationBar setBackgroundColor for changing color. We need to create image and use with setBackgroundImage:forBarMetrics: . So, I am using UIBezierPath to draw the background image instead of using png image.

-(UIImage*)barBackgroundImage{

    static UIImage *defaultImage = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 44), NO, 0.0f);

        [[UIColor grayColor] setFill];
        [[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 44)] fill];

        defaultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

    });

    return defaultImage;

}

-(void)setupUI
{
    [[UINavigationBar appearance] setBackgroundImage:[self barBackgroundImage] forBarMetrics:UIBarMetricsDefault];

}
comments powered byDisqus