IOS开发之自定义状态条
2024-07-04 11:33:58 223人阅读
第一种方法:
-(void)setRefreshWindow{
CGRect frame = CGRectMake(0.0, 0.0, 320.0, 20.0);
statusbarWindow = [[UIWindow alloc] initWithFrame:frame];
[statusbarWindow setBackgroundColor:[UIColor clearColor]];
[statusbarWindow setWindowLevel:UIWindowLevelStatusBar+1.0f];
// 添加自定义子视图
UIImageView *customView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 120, 18)];
customView.image=[UIImage imageNamed:@"数据刷新栏.png"];
// UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 100, 20)];
// // label.backgroundColor=[UIColor clearColor];
// label.text=@"数据正在刷新";
// [customView addSubview:label];
[statusbarWindow addSubview:customView];
[statusbarWindow makeKeyAndVisible];
}
第二种方法:
如果需要在状态栏显示自定义的消息时,就需要自定义状态栏。
代码如下:
XYCustomStatusBar.h
01 | #import <UIKit/UIKit.h> |
03 | @interface XYCustomStatusBar : UIWindow{ |
05 | UILabel *_messageLabel; |
08 | - ( void )showStatusMessage:(NSString *)message; |
XYCustomStatusBar.m
01 | #import "XYCustomStatusBar.h" |
03 | @implementation XYCustomStatusBar |
07 | [_messageLabel release], _messageLabel = nil; |
13 | self.frame = [UIApplication sharedApplication].statusBarFrame; |
14 | self.backgroundColor = [UIColor blackColor]; |
15 | self.windowLevel = UIWindowLevelStatusBar + 1.0f; |
17 | _messageLabel = [[UILabel alloc] initWithFrame:self.bounds]; |
18 | [_messageLabel setTextColor:[UIColor whiteColor]]; |
19 | [_messageLabel setTextAlignment:NSTextAlignmentRight]; |
20 | [_messageLabel setBackgroundColor:[UIColor clearColor]]; |
21 | [self addSubview:_messageLabel]; |
27 | - ( void )showStatusMessage:(NSString *)message{ |
30 | _messageLabel.text = @ "" ; |
32 | CGSize totalSize = self.frame.size; |
33 | self.frame = (CGRect){ self.frame.origin, 0, totalSize.height }; |
35 | [UIView animateWithDuration:0.5 animations:^{ |
36 | self.frame = (CGRect){self.frame.origin, totalSize }; |
37 | } completion:^( BOOL finished){ |
38 | _messageLabel.text = message; |
47 | [UIView animateWithDuration:0.5f animations:^{ |
49 | } completion:^( BOOL finished){ |
50 | _messageLabel.text = @ "" ; |
为了让自定义的状态栏可以让用户看到,设置了它的windowlevel,在ios中,windowlevel属性决定了UIWindow的显示层次,默认的windowlevel为UIWindowLevelNormal,即0.0 。为了能覆盖默认的状态栏,将windowlevel设置高点。其他代码基本上都不解释什么,如果要特殊效果,可以自己添加。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉:
投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。