当前位置:首页 > 通信资讯 > 正文

ios uiwindow(ios statusbar 颜色)

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

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

?
1 2 - (UIStatusBarStyle)preferredStatusBarStyle​ - (void)setNeedsStatusBarAppearanceUpdate​

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

上代码(demo部分代码):

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 // 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的设置问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/dls18/article/details/60756086

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。