1
2
3
4
5
6
7
8
9
|
@interface CustomStatusBar
:
UIWindow
{
UILabel
*
_messageLabel;
}
-
(
void
)
showStatusMessage
:
(
NSString
*
)
message
;
-
(
void
)
hide
;
@
end
|
1
2
|
self.frame
=
[UIApplication sharedApplication].statusBarFrame;
self.backgroundColor
=
[UIColor blackColor];
|
到这里,为了让自定义的状态栏可以让用户看到,还需要设置它的windowLevel。
1
2
3
4
|
const UIWindowLevel UIWindowLevelNormal;
const UIWindowLevel UIWindowLevelAlert;
const UIWindowLevel UIWindowLevelStatusBar;
typedef CGFloat UIWindowLevel;
|
为了能够覆盖系统默认的状态栏,我们把自定义的状态栏的windowLevel调高点:
1
|
self.windowLevel
=
UIWindowLevelStatusBar
+
1.0
f;
|
最后,为显示信息和隐藏添加一点无伤大雅的动画:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
-
(
void
)
showStatusMessage
:
(
NSString
*
)
message
{
self.
hidden
=
NO;
self.alpha
=
1.0
f;
_messageLabel.
text
=
@
""
;
CGSize totalSize
=
self.frame.size;
self.frame
=
(
CGRect
)
{
self.frame.origin
,
0
,
totalSize.height
}
;
[UIView animateWithDuration
:
0.5
f animations
:
^
{
self.frame
=
(
CGRect
)
{
self.frame.origin
,
totalSize
}
;
}
completion
:
^
(
BOOL finished
)
{
_messageLabel.
text
=
message
;
}
];
}
-
(
void
)
hide
{
self.alpha
=
1.0
f;
[UIView animateWithDuration
:
0.5
f animations
:
^
{
self.alpha
=
0.0
f;
}
completion
:
^
(
BOOL finished
)
{
_messageLabel.
text
=
@
""
;
self.
hidden
=
YES;
}
];;
}
|
完整源码:https://github.com/ZhiShiDangPu/CustomStatusBar
作者:Jason Lee
原文地址:http://blog.csdn.net/jasonblog