最近在做開發(fā)時(shí)要做一個(gè)類似于UIAlertView的控件,做法是創(chuàng)建一個(gè)基于UIView的類,在里面進(jìn)行自定義控件的設(shè)置,為了盡量模仿UIAlertView,在這個(gè)類里面創(chuàng)建了一個(gè)新的UIWindow并將self顯示到這個(gè)window上。
由于app中statusbar中的內(nèi)容為白色的,新創(chuàng)建的window就會(huì)改變statusbar的狀態(tài),不能得到我們想要的結(jié)果,為了避開一系列其他錯(cuò)誤,設(shè)置statusbar的顏色時(shí)采用
- (UIStatusBarStyle)preferredStatusBarStyle- (void)setNeedsStatusBarAppearanceUpdate
這兩個(gè)方法,你沒(méi)看錯(cuò),這兩個(gè)方法是UIViewController的方法,那么跟UIView有什么關(guān)系呢,不錯(cuò),這里我們是想給UIWindow設(shè)置rootViewController,在這個(gè)rootViewController中寫這兩個(gè)方法并做一些其他的設(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;}
代碼很簡(jiǎn)單,卻是我為了達(dá)到這一效果想了很久,感覺(jué)會(huì)有更簡(jiǎn)單的辦法,以后繼續(xù)研究。
以上所述是小編給大家介紹的iOS中關(guān)于UIWindow和statusbar的設(shè)置問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選