UINavigationBar Background Color

范哲
2023-12-01

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];

}

 
 
 类似资料:

相关阅读

相关文章

相关问答