当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS中关于UIWindow和statusbar的设置问题

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

最近在做开发时要做一个类似于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的设置问题,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网