iOS Madness detailed custom status bar code parsing

Source: Internet
Author: User

This explains that Apple does not have an open status bar API, and there is no mention in the official iOS documentation of how to modify window level;

First look at the available values for window level include:

1:typedef CGFloat Uiwindowlevel;

2:const Uiwindowlevel Uiwindowlevelnormal; 0.0

3:const Uiwindowlevel Uiwindowlevelalert; 2000.0

4:const Uiwindowlevel Uiwindowlevelstatusbar; 1000.0

By default our UIView layer are all on the Uiwindowlevelnormal, which is why the dialog box that comes out of the system is above our view because its window level is higher.

According to the principle of windowlevel, we also know that if you want to add a custom status bar to the status bar of the system, you need to be higher than the uiwindowlevelstatusbar level, and then, in code, explain:

First, build a single View application, and the name customization is OK,

Then, create a new class named: Statusbaroverlay inherits from UIWindow class, code:

StatusBarOverlay.h file

1: #import

2:

3: @interface statusbaroverlay:uiwindow{

4:uiview *contentview;

5:uilabel *textlabel;

6:}

7:

8: @property (nonatomic, retain) UIView *contentview;

9:

@property (nonatomic, retain) Uilabel *textlabel;

11:

@end

STATUSBAROVERLAY.M file

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:

One: #define Status_bar_orientation [UIApplication sharedapplication].statusbarorientation

#define Rotation_animation_duration [UIApplication sharedapplication].statusbarorientationanimationduration

13:

14:

@interface Statusbaroverlay ()

16:

:-(void) initializetodefaultstate;

:-(void) Rotatestatusbarwithframe: (Nsvalue *) Framevalue;

:-(void) setsubviewhframe;

:-(void) setsubviewvframe;

@end

22:

23:

Statusbaroverlay: @implementation

: @synthesize Contentview;

Num: @synthesize Textlabel;

27:

28://Rewrite Init method

:-(ID) init

30: {

31:self = [Super Initwithframe:cgrectzero];

32:if (self) {

33:self.windowlevel = Uiwindowlevelstatusbar + 1;

34:self.frame = [UIApplication sharedapplication].statusbarframe;

[Self Setbackgroundcolor:[uicolor orangecolor]];

[Self sethidden:no];

37:

38://Content View

39:uiview *_contentview = [[UIView alloc] initWithFrame:self.bounds];

40:self.contentview = _contentview;

: [Self.contentview setautoresizingmask:uiviewautoresizingflexiblewidth];

: [Self.contentview Setbackgroundcolor:[uicolor Cyancolor]];

[Self addSubview:self.contentView];

: [_contentview release];

45:

46:

47://Add Textlabel

48:uilabel *_textlabel = [[Uilabel alloc] Initwithframe:cgrectmake (0, Cgrectgetwidth (self.frame)-60, Cgrectgetheight (Self.frame))];

49:self.textlabel = _textlabel;

: [Self.textlabel Setbackgroundcolor:[uicolor Bluecolor]];

"[Self.textlabel Setfont:[uifont systemfontofsize:12]";

: [Self.textlabel Settextalignment:uitextalignmentcenter];

: [Self.textlabel Settextcolor:[uicolor Blackcolor]];

[Self.textlabel settext:@ "Custom status bar author by Jordy"];

: [Self.contentview AddSubview:self.textLabel];

A: [_textlabel release];

57:

58://Registration Listener---event (for manipulating this view to change its frame) when the screen is going to rotate

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (willrotatescreenevent:) Name: Uiapplicationwillchangestatusbarframenotification Object:nil];

60://Initialization

: [Self initializetodefaultstate];

62:}

63:

64:return self;

65:}

66:

67:

68:

69:

70://initialized to default state

:-(void) initializetodefaultstate

72: {

73://Get the current status bar position

74:cgrect statusbarframe = [UIApplication sharedapplication].statusbarframe;

75://Set the rotation of the current view, depending on the current device orientation

: [Self rotatestatusbarwithframe:[nsvalue valuewithcgrect:statusbarframe]];

77:

78:

79:

80:}

81:

82:

83://Rotate Screen

The:-(void) Rotatestatusbarwithframe: (Nsvalue *) framevalue

85: {

86:cgrect frame = [Framevalue cgrectvalue];

87:uiinterfaceorientation orientation = status_bar_orientation;

88:

89:if (Orientation = = uideviceorientationportrait) {

90:self.transform = cgaffinetransformidentity; Screen does not rotate

A: [Self setsubviewvframe];

:}else if (orientation = = Uideviceorientationportraitupsidedown) {

93:self.transform = Cgaffinetransformmakerotation (M_PI); The screen rotates 180 degrees

: [Self setsubviewvframe];

:}else if (orientation = = Uideviceorientationlandscaperight) {

96:self.transform = Cgaffinetransformmakerotation ((M_PI * ( -90.0f)/180.0f)); Screen rotation-90 degrees

: [Self setsubviewhframe];

:}else if (orientation = = Uideviceorientationlandscapeleft) {

99:self.transform = cgaffinetransformmakerotation (M_PI * 90.0f/180.0f); The screen rotates 90 degrees

[Self setsubviewhframe];

101:}

102:

103:self.frame = frame;

[Self.contentview SetFrame:self.bounds];

105:}

106:

107://Set the frame of the child view of the horizontal screen

108:-(void) setsubviewhframe

109: {

110:self.textlabel.frame = CGRectMake (30, 0, 1024-60, 20);

111:}

112://Set the frame of the child view of the vertical screen

113:-(void) setsubviewvframe

114: {

115:self.textlabel.frame = CGRectMake (30, 0, 748-60, 20);

116:}

117:

118: #pragma mark-

119: #pragma mark responds to events when the screen is about to rotate

Of:-(void) Willrotatescreenevent: (nsnotification *) notification

121: {

122:nsvalue *framevalue = [Notification.userinfo Valueforkey:uiapplicationstatusbarframeuserinfokey];

123: [Self rotatestatusbaranimatedwithframe:framevalue];

124:}

125:

126:-(void) Rotatestatusbaranimatedwithframe: (Nsvalue *) Framevalue {

127: [UIView animatewithduration:rotation_animation_duration animations:^{

128:self.alpha = 0;

129:} completion:^ (BOOL finished) {

130: [Self rotatestatusbarwithframe:framevalue];

131: [UIView animatewithduration:rotation_animation_duration animations:^{

132:self.alpha = 1;

133:}];

134:}];

135:}

136:

137:-(void) dealloc

138: {

139: [[Nsnotificationcenter Defaultcenter] removeobserver:self];

140: [Textlabel release];

141:textlabel = nil;

142:

143: [Contentview release];

144:contentview = nil;

145:

146: [Super Dealloc];

147:}

148:

149: @end

Since the code is relatively simple, and I have a corresponding comment in the above code, here is the point is that the default we inherited from the UIWindow Statusbaroverlay class is the hidden state, the need to set its hidden property is no when initialized,

In the screen rotation process, the custom status bar and the rotation between the uiviewcontroller is separate, so we need to do a hidden animation, before the rotation process to hide the custom status bar, rotate the results and set the display state.

If you need to do an animation, such as moving from the bottom to display a piece of information, after n seconds and automatically retract the animation, directly set the custom view of the y-coordinate, the default y-coordinate setting is 0.

Finally, the way to use it is relatively simple, just initialize, code:

Statusbaroverlay *statusbaroverlay = [[Statusbaroverlay alloc] init];

Because my company's demand is the automatic download function, so I in the initialization, is placed in the appdelegate.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.