公司的開發的項目要求在狀態欄上邊加入程序下載的進度條,之前寫的程序,由于是根據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;
新聞熱點
疑難解答