给 UINavigationBar 设置背景图片的最简单方法

来源:互联网 发布:淘宝买东西怎么付款 编辑:程序博客网 时间:2024/04/30 20:41

 

利用objective-c的Category语法 扩展UINavigationBar 类

具体代码为

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
    // Drawing code
UIImage *img = [UIImage imageNamed: @"navbar_background.png"];
CGPoint point = {0,0};

[img drawAtPoint:point];


}
@end

 

////////////////////方法2////////////////////////////////////////////////////

 

@implementation UINavigationBar (UINavigationBarCategory)

 

- (void)drawRect:(CGRect)rect {

//加入旋转坐标系代码

    // Drawing code

UIImage *navBarImage = [UIImage imageNamed:@"LOGO_320×44.png"];

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0.0self.frame.size.height);

CGContextScaleCTM(context, 1.0, -1.0);

CGPoint center=self.center;

 

CGImageRef cgImage= CGImageCreateWithImageInRect(navBarImage.CGImage,CGRectMake(00144));

CGContextDrawImage(context, CGRectMake(center.x-160-80080,self.frame.size.height), cgImage);

CGContextDrawImage(context, CGRectMake(center.x-1600320,self.frame.size.height), navBarImage.CGImage);

CGContextDrawImage(context, CGRectMake(center.x+160080,self.frame.size.height), cgImage);

 

}

 

@end

 

old code

 

CGContextDrawImage(context, CGRectMake(00self.frame.size.width,self.frame.size.height), navBarImage.CGImage);

 

原创粉丝点击