ios中uiwindow statusbar iOS中关于UIWindow和statusbar的设置问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

ios中uiwindow statusbar iOS中关于UIWindow和statusbar的设置问题

小明0326   2021-03-24 我要评论
想了解iOS中关于UIWindow和statusbar的设置问题的相关内容吗,小明0326在本文为您仔细讲解ios中uiwindow statusbar的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:ios,uiwindow,statusbar,下面大家一起来学习吧。

最近在做开发时要做一个类似于UIAlertView的控件,做法是创建一个基于UIView的类,在里面进行自定义控件的设置,为了尽量模仿UIAlertView,在这个类里面创建了一个新的UIWindow并将self显示到这个window上。

由于app中statusbar中的内容为白色的,新创建的window就会改变statusbar的状态,不能得到我们想要的结果,为了避开一系列其他错误,设置statusbar的颜色时采用

- (UIStatusBarStyle)preferredStatusBarStyle​
- (void)setNeedsStatusBarAppearanceUpdate​

这两个方法,你没看错,这两个方法是UIViewController的方法,那么跟UIView有什么关系呢,不错,这里我们是想给UIWindow设置rootViewController,在这个rootViewController中写这两个方法并做一些其他的设置,这样控件能显示在rootViewController中,statusbar也是我们想要的状态。

上代码(demo部分代码):

// TestView.h
#import
@interface TestView : UIView
@property (nonatomic, strong) NSString *clickTitle;​
- (void)show;
@end
// TestView.m
​#import "TestView.h"
@implementation TestView
{
  UIWindow *window;
  UIButton *clickBtn;
}
- (void)makeWindow
{
  window = [[UIWindow alloc] init];
  window.windowLevel = UIWindowLevelStatusBar + 1;
  window.backgroundColor = [UIColor clearColor];
  [window makeKeyAndVisible];
  TestVC *rootVC = [[TestVCalloc] init];
  rootVC.view.backgroundColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.5];
  UINavigationController *navi = [[UINavigationControlleralloc]initWithRootViewController:rootVC];
  window.rootViewController = navi;
  navi.navigationBarHidden = YES;
  self.frame = CGRectMake(0, 0, window.frame.size.width - 40, 80);
  self.center = window.center;
  [rootVC.view addSubview:self];
}
- (instancetype)initWithFrame:(CGRect)frame
{
  if (self = [super initWithFrame:frame]) {
    clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  }
  returnself;
}
- (NSString *)clickTitle
{
  if (!_clickTitle) {
    _clickTitle = @"clicks";
  }
  return_clickTitle;
}
- (void)show
{
  [clickBtn setTitle:self.clickTitleforState:UIControlStateNormal];
  [selfmakeWindow];
}
// TestVC.m
#import "TestVC.h"
@interfaceTestVC ()
@end
@implementation TestVC
- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  [self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
  return UIStatusBarStyleLightContent;
}

​代码很简单,却是我为了达到这一效果想了很久,感觉会有更简单的办法,以后继续研究。

以上所述是小编给大家介绍的iOS中关于UIWindow和statusbar的设置问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们