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

来源:互联网 发布:手机淘宝店店招图片 编辑:程序博客网 时间:2024/06/05 04:21

不多说,上例子:

#import <Foundation/Foundation.h>//自定义状态栏,状态栏显示灰色背景并【indicator message】。用于耗时操作的状态栏信息提示//例如:访问网络时,提示正在获取网络数据,或者正在提交数据至服务器等提示@interface CHStatusBar : UIWindow {@privateUILabel *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开发的zoomself.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];//创建一个progressindicator = [[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

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

效果如下:


原创粉丝点击