iOS Madness detailed custom status bar code parsing

Source: Internet
Author: User

The company's development of the project requires the status bar to join the program download progress bar, previously written program, because it is based on the direction of the ipad to set the custom status bar frame, as well as the sub-view frame and transform, there are some less easy to solve the bug. These two days just the project is not too tight, just learn this knowledge, the following is a little experience I have summed up:

This explains that Apple does not have an open status bar API, and the official iOS document does not mention the way to modify the window level;

Take a 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 is on the uiwindowlevelnormal, which is why the system bounces out the dialog on top of our view because it has a higher window level.

According to the principle of windowlevel, we also know that if you want to add a custom status bar on the system's status bar, you need a higher level than the Uiwindowlevelstatusbar, and then use the code to illustrate it:

First of all, build a single View application, the name of the custom can be,

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

StatusBarOverlay.h file

1: #import <Foundation/Foundation.h>
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:
: @implementation Statusbaroverlay
: @synthesize Contentview;
: @synthesize Textlabel;
27:
28://Overriding the Init method
:-(ID) init
30: {
To: Self  = [Super Initwithframe:cgrectzero];
:  if (self) {
:  self.windowlevel = Uiwindowlevelstatusbar + 1;
:  self.frame = [UIApplication sharedapplication].statusbarframe;
:  [self setbackgroundcolor:[uicolor orangecolor]];
:  [self sethidden:no];
Section:  //Content View
:  UIView *_contentview = [[UIView alloc] initWithFrame:self.bounds];
Max:  self.contentview = _contentview;
In:  [Self.contentview setautoresizingmask:uiviewautoresizingflexiblewidth];
£ º  [Self.contentview Setbackgroundcolor:[uicolor Cyancolor]];
:  [self addSubview:self.contentView];
:  [_contentview release];
:  //Add Textlabel
Max:  UILabel *_textlabel = [[UILabel alloc] Initwithframe:cgrectmake (0, Cgrectgetwidth (self.frame)-60, Cgrectgetheight (Self.frame))];
£  self.textlabel = _textlabel;
:  [Self.textlabel Setbackgroundcolor:[uicolor Bluecolor]];
Wuyi:  [Self.textlabel Setfont:[uifont Systemfontofsize:12]];
:  [Self.textlabel settextalignment:uitextalignmentcenter];
:  [Self.textlabel Settextcolor:[uicolor Blackcolor]];
Si:  [self.textlabel settext:@ "Custom status bar author by Jordy"];
:  [Self.contentview AddSubview:self.textLabel];
:  [_textlabel release];
+  //Register Monitor---when the screen is about to turn, the event that was started (to manipulate this view to change its frame)
:  [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (willrotatescreenevent:) Name: Uiapplicationwillchangestatusbarframenotification Object:nil];
Max:  //initialization
A:  [self initializetodefaultstate];
+:  }
+:  return self;
65:}
66:
67:
68:
69:
70://Initialize to default state
Initializetodefaultstate:-(void)
72: {
:  //Get the current status bar position
:  cgrect statusbarframe = [UIApplication sharedapplication].statusbarframe;
:  //Sets the rotation of the current view, depending on the current device orientation
:  [self rotatestatusbarwithframe:[nsvalue valuewithcgrect:statusbarframe]];
80:}
81:
82:
83://Rotate the screen
:-(void) Rotatestatusbarwithframe: (Nsvalue *) framevalue
85: {
:  CGRect frame = [Framevalue cgrectvalue];
:  uiinterfaceorientation orientation = status_bar_orientation;
:  if (orientation = = uideviceorientationportrait) {
:  self.transform = cgaffinetransformidentity;//screen does not rotate
A:  [self setsubviewvframe];
:  }else if (orientation = = Uideviceorientationportraitupsidedown) {
:  self.transform = cgaffinetransformmakerotation (M_PI);//screen rotation 180 degrees
94:  [Self setsubviewvframe];
:  }else if (orientation = = Uideviceorientationlandscaperight) {
:  Self.transform = cgaffinetransformmakerotation ((M_PI * ( -90.0f)/180.0f));//Screen rotation-90 degrees
:  [self setsubviewhframe];
98:  }else if (orientation = = Uideviceorientationlandscapeleft) {
:  self.transform = cgaffinetransformmakerotation (M_PI * 90.0f/180.0f);//Screen rotation 90 degrees
:  [self setsubviewhframe];
101:  }
103:  self.frame = frame;
104:  [Self.contentview setFrame:self.bounds];
105:}
106:
107://Set the frame of the sub-view of the horizontal screen
108:-(void) setsubviewhframe
109: {
:  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: {
:  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
:-(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:^{
:  self.alpha = 0;
129:  } completion:^ (BOOL finished) {
:  [self rotatestatusbarwithframe:framevalue];
131:  [UIView animatewithduration:rotation_animation_duration animations:^{
:  self.alpha = 1;
133:  }];
134:  }];
135:}
136:
137:-(void) dealloc
138: {
139:  [[Nsnotificationcenter Defaultcenter] removeobserver:self];
:  [Textlabel release];
141:  Textlabel = nil;
143:  [Contentview release];
144:  Contentview = nil;
146:  [Super Dealloc];
147:}
148:
149: @end

Because the code is relatively simple, and I have a corresponding comment in the above code, it should be noted that the default is that we inherit from the UIWindow Statusbaroverlay class is the hidden state, need to set its hidden property at initialization is no,

During the screen rotation, the rotation between the custom status bar and the Uiviewcontroller is separate, so we need to make a hidden animation, hide the custom status bar before the rotation process, and set the display state after the result is rotated.

If you need to do an animation, such as moving from the bottom down to display a message, 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 also relatively simple, only need to initialize, code:

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

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

iOS Madness detailed custom status bar code parsing

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.