Tuesday, March 19, 2013

navigationBarとtoolBarを透明にする

navigationbar, toolbarをUIBarStyleBlackTranslucent以外で透明にする方法

barのbackgroundimageに透明なimageを設定
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.0f alpha:0.8f]] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.toolBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithWhite:0.0f alpha:0.8f]] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];

透明なimageはどう生成してもいいんですが、ここでは以下のカテゴリで作成している
#import "UIImage+Color.h"

@implementation UIImage (Color)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
        
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
        
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
        
    return image;
}

@end

No comments:

Post a Comment