iOS中關(guān)于UIWindow和statusbar的設(shè)置問題
最近在做開發(fā)時要做一個類似于UIAlertView的控件,做法是創(chuàng)建一個基于UIView的類,在里面進行自定義控件的設(shè)置,為了盡量模仿UIAlertView,在這個類里面創(chuàng)建了一個新的UIWindow并將self顯示到這個window上。
由于app中statusbar中的內(nèi)容為白色的,新創(chuàng)建的window就會改變statusbar的狀態(tài),不能得到我們想要的結(jié)果,為了避開一系列其他錯誤,設(shè)置statusbar的顏色時采用
- (UIStatusBarStyle)preferredStatusBarStyle - (void)setNeedsStatusBarAppearanceUpdate
這兩個方法,你沒看錯,這兩個方法是UIViewController的方法,那么跟UIView有什么關(guān)系呢,不錯,這里我們是想給UIWindow設(shè)置rootViewController,在這個rootViewController中寫這兩個方法并做一些其他的設(shè)置,這樣控件能顯示在rootViewController中,statusbar也是我們想要的狀態(tài)。
上代碼(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; }
代碼很簡單,卻是我為了達到這一效果想了很久,感覺會有更簡單的辦法,以后繼續(xù)研究。
以上所述是小編給大家介紹的iOS中關(guān)于UIWindow和statusbar的設(shè)置問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法
UIDevice最常見的用法就是用來監(jiān)測iOS設(shè)備的電量了,然后再實現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來總結(jié)一下iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法:2016-07-07詳解Obejective-C中將JSON數(shù)據(jù)轉(zhuǎn)為模型的方法
這篇文章主要介紹了Obejective-C中JSON數(shù)據(jù)轉(zhuǎn)為模型的方法,同時介紹了使用jastor庫的方法,需要的朋友可以參考下2016-03-03