公司的开发的项目要求在状态栏上边加入程序下载的进度条,之前写的程序,由于是根据ipad的朝向来设置自定义的状态栏的frame,以及子视图的 frame和transform,出现一些不太容易解决的bug。这两天正好项目不太紧,就好好学习一下这方面的知识,以下是我所总结的一点经验:
这里说明一下,Apple没有开放的状态栏的API,在ios 的官方文档没有提到修改Window Level的方式;
先看一下Window Level的可用的值包括:
1: typedef CGFloat UIWindowLevel;
2: const UIWindowLevel UIWindowLevelNormal; // 0.0
3: const UIWindowLevel UIWindowLevelAlert; // 2000.0
4: const UIWindowLevel UIWindowLevelStatusBar; // 1000.0
默认我们的UIView layer都是在UIWindowLevelNormal上,这也就是为什么系统弹出来的对话框在我们的视图之上,因为它的Window Level级别更高。
根据WindowLevel的原理我们也就知道,如果想在系统的状态栏上,添加自定义的状态栏,就需要比UIWindowLevelStatusBar的级别更高,接下来,用代码说明一下:
首先,先建一个Single View Application,名字自定义就可以了,
然后,新建一个类命名为: StatusBarOverlay 继承自UIWindow类,代码:
StatusBarOverlay.h文件
1: #import
2:
3: @interface StatusBarOverlay : UIWindow{
4: UIView *contentView;
5: UILabel *textLabel;
6: }
7:
8: @property (nonatomic, retain) UIView *contentView;
9:
10: @property (nonatomic, retain) UILabel *textLabel;
11:
12: @end
StatusBarOverlay.m文件
1: //
2: // StatusBarOverlay.m
3: // StatusBarDemo
4: //
5: // Created by jordy wang on 12-8-7.
6: // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
7: //
8:
9: #import "StatusBarOverlay.h"
10:
11: #define STATUS_BAR_ORIENTATION [UIApplication sharedApplication].statusBarOrientation
12: #define ROTATION_ANIMATION_DURATION [UIApplication sharedApplication].statusBarOrientationAnimationDuration
13:
14:
15: @interface StatusBarOverlay()
16:
17: - (void)initializeToDefaultState;
18: - (void)rotateStatusBarWithFrame:(NSValue *)frameValue;
19: - (void)setSubViewHFrame;
20: - (void)setSubViewVFrame;
21: @end
22:
23:
24: @implementation StatusBarOverlay
25: @synthesize contentView;
26: @synthesize textLabel;
27:
28: //重写init方法
29: - (id)init
30: {
31: self = [super initWithFrame:CGRectZero];
32: if (self) {
33: self.windowLevel = UIWindowLevelStatusBar + 1;
34: self.frame = [UIApplication sharedApplication].statusBarFrame;
35: [self setBackgroundColor:[UIColor orangeColor]];
36: [self setHidden:NO];
37:
38: //内容视图
39: UIView *_contentView = [[UIView alloc] initWithFrame:self.bounds];
40: self.contentView = _contentView;
新闻热点
疑难解答