最近在做開發時要做一個類似于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的設置問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答