Corona中文站

强大、易学的跨平台(iOS/Android)开发框架。QQ群1:74390406(满) 群2:221929599

导航

自定义UINavigationBar背景图片
The solution below works well for me:

.h::::

#import


@interface MTNavigationBar : UINavigationBar
{
UIImageView *bg;
}
-(void)setBackgroundImage:(UIImage*)image;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
@end


.m::::
#import "MTNavigationBar.h"

@implementation MTNavigationBar

-(void)setBackgroundImage:(UIImage*)image {
if(image == nil) return;
bg = [[UIImageView alloc]initWithImage:image];
bg.frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
[self addSubview:bg];
[self sendSubviewToBack:bg];
[bg release];
}

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
[super insertSubview:view atIndex:index];
[self sendSubviewToBack:bg];
}

@end


In your own VC:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

UINavigationBar* bar = self.navigationController.navigationBar;
[bar setBackgroundImage:[UIImage imageNamed:@"nav_top.png"] withTag:9999];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[self.navigationController.navigationBar resetBackground:9999];
}

上面的吊办法不行啊
还是用下面这样直接绘制吧
@interface UINavigationBar(clm)
-(void)drawRect:(CGRect)rect;
@end

@implementation UINavigationBar(clm)
-(void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:@"3.png"];
[image drawInRect:rect];
}
加在.h文件里面就可以了
<< 用xCode Leaks检查iPhone/iPad程序内存泄露使用uilabel实现跑马灯效果 >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Copyright 2011-2015 BuildApp.Net. All Rights Reserved.