iOS Status bar action

Source: Internet
Author: User

[CPP]View Plain copy print?
    1. [UIApplication sharedapplication].networkactivityindicatorvisible = YES; Show
    2. [UIApplication sharedapplication].networkactivityindicatorvisible = NO; Hide
Let the status bar display the network wait flag

The status bar can be modified by some of the methods provided by the UIApplication class, such as completely removing the status bar or modifying the style, but these changes are only within your program and will be restored when you exit your program.


    1. UIApplication *myapp = [UIApplication sharedapplication];
Copy Code

1. Hide the status bar


    1. [MyApp Setstatusbarhidden:yes Animated:yes];
Copy Code

Remember to hide the status bar after your "desktop" increases the size of the 320x20, so it is best to hide it before any window or view creation.

2. Status bar Style

    1. [MyApp Setstatusbarstyle:uistatusbarstyleblackopaque];
Copy Code
    1. typedef enum {
    2. Uistatusbarstyledefault,
    3. Uistatusbarstyleblacktranslucent,
    4. Uistatusbarstyleblackopaque
    5. } Uistatusbarstyle;
Copy code 3. Status Bar Direction
    1. [MyApp Setstatusbarorientation:uiinterfaceorientationlandscapeleft Animated:no];
Copy Code
    1. typedef enum {  
    2.      UIInterfaceOrientationPortrait  &nbs p;        = uideviceorientationportrait,  
    3.       //vertical screen, vertical upward   
    4.      uiinterfaceorientationportraitupsidedown = UIDEVICEORIENTATIONPORTRAITUPSIDEDOWN, &NBSP
    5.      //vertical screen, vertically upside down   &NBSP
    6.      UIInterfaceOrientationLandscapeLeft      = UIDEVICEORIENTATIONLANDSCAPERIGHT, &NBSP
    7.      //device rotate counterclockwise to horizontal screen mode   
    8.      UIInterfaceOrientationLandscapeRight     = UIDEVICEORIENTATIONLANDSCAPELEFT &NBSP
    9.      //device rotate clockwise to landscape mode   
    10.    } uiinterfaceorientation;  
Copy Code

Sometimes it is necessary to display some custom information on the status bar, such as the official iOS client of Sina Weibo: informing the user that the message is in the sending queue, sending it successfully, or failing to send it.

For example, by displaying custom information in the status bar, you can be user-friendly without compromising the software's use of hints.

To do this, we appear to define a custom status bar class that contains a label that displays information:

[CPP]View Plain Copy
    1. @interface Customstatusbar:uiwindow
    2. {
    3. UILabel *_messagelabel;
    4. }
    5. -(void) Showstatusmessage: (NSString *) message;
    6. -(void) hide;
    7. @end

Next, set the size to match the system status bar and the background is black: [CPP]View Plain Copy
    1. Self.frame = [UIApplication sharedapplication].statusbarframe;
    2. Self.backgroundcolor = [Uicolor blackcolor];

Here, in order for the custom status bar to be visible to the user, it is also necessary to set its windowlevel.

In iOS, the Windowlevel property determines the level of display for UIWindow. The default windowlevel is Uiwindowlevelnormal, which is 0.0.

The system defines three levels as follows, with reference to the official documentation:

[CPP]View Plain Copy
    1. Const Uiwindowlevel Uiwindowlevelnormal;
    2. Const Uiwindowlevel Uiwindowlevelalert;
    3. Const Uiwindowlevel Uiwindowlevelstatusbar;
    4. typedef cgfloat Uiwindowlevel;

To be able to override the system's default status bar, we windowlevel the custom status bar: [CPP]View Plain Copy
    1. Self.windowlevel = Uiwindowlevelstatusbar + 1.0f;

Finally, add a little harmless animation for displaying information and hiding: [CPP]View Plain Copy
  1. -(void) showstatusmessage: (NSString *) message
  2. {  
  3. Self.hidden = NO;
  4. Self.alpha = 1.0f;
  5. _messagelabel.text = @"";
  6.       
  7. cgsize totalsize = self.frame.size;
  8. self.frame = (cgrect) {self.frame.origin, 0, totalsize.height};
  9.       
  10. [UIView animatewithduration:0.5f animations:^{
  11. self.frame = (cgrect) {self.frame.origin, totalsize};
  12. } completion:^ (BOOL finished) {
  13. _messagelabel.text = message;
  14.     }];
  15. }  
  16.   
  17. -(void) Hide
  18. {  
  19. Self.alpha = 1.0f;
  20.       
  21. [UIView animatewithduration:0.5f animations:^{
  22. Self.alpha = 0.0f;
  23. } completion:^ (BOOL finished) {
  24. _messagelabel.text = @"";
  25. Self.hidden = YES;
  26.     }];;
  27. }  

iOS status bar actions

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.