分享一个简单的iphone自定义状态栏例子

不多说,上例子:

#import <Foundation/Foundation.h>


//自定义状态栏,状态栏显示灰色背景并【indicator message】。用于耗时操作的状态栏信息提示
//例如:访问网络时,提示正在获取网络数据,或者正在提交数据至服务器等提示
@interface CHStatusBar : UIWindow {
	@private
		UILabel *lblStatus;
		UIActivityIndicatorView *indicator;
}


-(void)showWithStatusMessage:(NSString*)msg;
-(void)hide;


@end


#import "CHStatusBar.h"


@implementation CHStatusBar


- (id) initWithFrame:(CGRect)frame{
	if (self = [super initWithFrame:frame]) {
		// 将窗体置于正确的位置和级别,就是比状态栏的级别稍高即可
		// 否则该窗体会被标准状态栏遮住,相当于web开发的zoom
		self.windowLevel = UIWindowLevelStatusBar + 1.0f;
		// 使窗体的框架和状态栏框架一致
		self.frame = [UIApplication sharedApplication].statusBarFrame;
		
		// 创建一个灰色图片背景,使他视觉上还是一个标准状态栏的感觉
		UIImageView* backgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
		backgroundImageView.image = [[UIImage imageNamed:@"statusbar_background.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:0];
		[self addSubview:backgroundImageView];
		[backgroundImageView release];
		
		//创建一个progress
		indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
		indicator.frame = (CGRect) {
			.origin.x = 2.0f, 
			.origin.y = 3.0f, 
			.size.width = self.frame.size.height - 6, 
			.size.height = self.frame.size.height - 6
		};
		indicator.hidesWhenStopped = YES;
		[self addSubview:indicator];
		
		//文字信息,用于和用户进行交互,最好能提示用户当前是什么操作
		lblStatus = [[UILabel alloc] initWithFrame:(CGRect){.origin.x = self.frame.size.height, .origin.y = 0.0f, .size.width = 200.0f, .size.height = self.frame.size.height}];
		lblStatus.backgroundColor = [UIColor clearColor];
		lblStatus.textColor = [UIColor blackColor];
		lblStatus.font = [UIFont boldSystemFontOfSize:10.0f];
		[self addSubview:lblStatus];		
	}
	return self;
}


- (void) showWithStatusMessage:(NSString*) msg {
	if (!msg) return;
	lblStatus.text = msg;
	[indicator startAnimating];
	self.hidden = NO;
}


- (void) hide {
	[indicator stopAnimating];
	self.hidden = YES;
}


- (void) dealloc {
	[lblStatus release];
	[indicator release];
	[super dealloc];
}


@end

状态栏的背景图片找了个灰色的:

效果如下:


阅读更多
个人分类: iphone开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭