Method 1:
-(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]; // Add custom sub-view 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 = @ "Data refreshing"; // [customView addSubview: label]; [statusbarWindow addSubview: customView]; [statusbarWindow makeKeyAndVisible];}
Method 2:
If you want to display custom messages in the status bar, you need to customize the status bar.
The Code is as follows:
XYCustomStatusBar. 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 = @""; |
In order to make the custom status bar visible to the user and set its windowlevel, in ios, The windowlevel attribute determines the display level of the UIWindow. The default windowlevel is UIWindowLevelNormal, that is, 0.0. To overwrite the default status bar, set windowlevel to a high point. Other code basically does not explain anything. If you want special effects, you can add them by yourself.