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;

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值