自定义iOS状态栏

移动开发 iOS
为了让自定义的状态栏可以让用户看到,设置了它的windowlevel,在ios中,windowlevel属性决定了UIWindow的显示层次,默认的windowlevel为UIWindowLevelNormal,即0.0 。为了能覆盖默认的状态栏,将windowlevel设置高点。其他代码基本上都不解释什么,如果要特殊效果,可以自己添加。

如果需要在状态栏显示自定义的消息时,就需要自定义状态栏。

代码如下:

XYCustomStatusBar.h

  1. #import <UIKit/UIKit.h>   
  2. @interface XYCustomStatusBar : UIWindow{   
  3.      UILabel *_messageLabel;   
  4.  }   
  5.  - (void)showStatusMessage:(NSString *)message;   
  6. - (void)hide;   
  7. @end  

XYCustomStatusBar.m

  1. #import "XYCustomStatusBar.h"   
  2.  @implementation XYCustomStatusBar       
  3.  - (void)dealloc{   
  4.     [super dealloc];   
  5.     [_messageLabel release], _messageLabel = nil;   
  6.  }       
  7.  - (id)init{   
  8.      self = [super init];   
  9.      if (self) {   
  10.          self.frame = [UIApplication sharedApplication].statusBarFrame;   
  11.          self.backgroundColor = [UIColor blackColor];   
  12.          self.windowLevel = UIWindowLevelStatusBar + 1.0f;   
  13.          _messageLabel = [[UILabel alloc] initWithFrame:self.bounds];   
  14.          [_messageLabel setTextColor:[UIColor whiteColor]];   
  15.          [_messageLabel setTextAlignment:NSTextAlignmentRight];   
  16.          [_messageLabel setBackgroundColor:[UIColor clearColor]];   
  17.          [self addSubview:_messageLabel];   
  18.      }   
  19.      return self;   
  20.  }   
  21.  - (void)showStatusMessage:(NSString *)message{   
  22.      self.hidden = NO;   
  23.      self.alpha = 1.0f;   
  24.      _messageLabel.text = @"";   
  25.      CGSize totalSize = self.frame.size;   
  26.      self.frame = (CGRect){ self.frame.origin, 0, totalSize.height };   
  27.      [UIView animateWithDuration:0.5 animations:^{   
  28.          self.frame = (CGRect){self.frame.origin, totalSize };   
  29.      } completion:^(BOOL finished){   
  30.          _messageLabel.text = message;   
  31.      }];   
  32.  }   
  33.  - (void)hide{   
  34.      self.alpha = 1.0f;           
  35.      [UIView animateWithDuration:0.5f animations:^{   
  36.          self.alpha = 0.0f;   
  37.      } completion:^(BOOL finished){   
  38.          _messageLabel.text = @"";   
  39.          self.hidden = YES;   
  40.      }];   
  41.  }   
  42.  @end  

为了让自定义的状态栏可以让用户看到,设置了它的windowlevel,在ios中,windowlevel属性决定了UIWindow的显示层次,默认的windowlevel为UIWindowLevelNormal,即0.0 。为了能覆盖默认的状态栏,将windowlevel设置高点。其他代码基本上都不解释什么,如果要特殊效果,可以自己添加。

责任编辑:闫佳明 来源: oschina
相关推荐

2012-12-24 14:42:48

iOS自定义状态栏

2014-06-06 14:03:13

iOS状态栏提示控件原理

2013-11-20 15:08:32

iOS开发状态栏

2021-08-24 15:25:59

鸿蒙HarmonyOS应用

2013-06-27 11:10:01

iOS开发自定义UISlider

2017-02-17 11:00:57

状态栏Android

2011-08-02 11:17:13

iOS开发 View

2021-01-20 08:58:39

iOS 14桌面图标快捷指令

2013-05-30 15:53:17

iOS开发iOS SDKPopver

2015-02-12 14:49:36

CGToast状态栏提示Status

2016-11-29 11:20:08

Android

2017-10-25 14:07:54

APPiOSxcode

2012-06-01 11:02:33

2011-05-04 10:40:02

网页加载进度标题栏lephone

2015-02-12 15:33:43

微信SDK

2015-01-15 16:45:05

iOS源码自定义画图

2022-07-15 16:39:46

ETS导航栏组件

2015-02-12 15:38:26

微信SDK

2017-02-08 20:21:03

Windows 10Windows任务栏

2015-10-12 16:47:13

iOS下拉线条动画
点赞
收藏

51CTO技术栈公众号