iOS中關(guān)于UIWindow和statusbar的設(shè)置問題
最近在做開發(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就會改變statusbar的狀態(tài),不能得到我們想要的結(jié)果,為了避開一系列其他錯(cuò)誤,設(shè)置statusbar的顏色時(shí)采用
- (UIStatusBarStyle)preferredStatusBarStyle - (void)setNeedsStatusBarAppearanceUpdate
這兩個(gè)方法,你沒看錯(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;
}
代碼很簡單,卻是我為了達(dá)到這一效果想了很久,感覺會有更簡單的辦法,以后繼續(xù)研究。
以上所述是小編給大家介紹的iOS中關(guān)于UIWindow和statusbar的設(shè)置問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS tableView實(shí)現(xiàn)搜索功能
這篇文章主要為大家詳細(xì)介紹了iOS tableView實(shí)現(xiàn)搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
ios實(shí)現(xiàn)tableView頂部彈簧圖片效果
這篇文章主要為大家詳細(xì)介紹了ios實(shí)現(xiàn)tableView頂部彈簧圖片效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
iOS 點(diǎn)擊圖片放大效果的實(shí)現(xiàn)
本篇文章主要介紹了iOS 點(diǎn)擊圖片放大效果的實(shí)現(xiàn),這種效果一般在微博,微信朋友圈中比較常見,有興趣的可以了解一下。2017-01-01
iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法
UIDevice最常見的用法就是用來監(jiān)測iOS設(shè)備的電量了,然后再實(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)為模型的方法,同時(shí)介紹了使用jastor庫的方法,需要的朋友可以參考下2016-03-03

