關(guān)于手動創(chuàng)建UIWindow的使用诗良,最近做項目遇到一個很是頭疼的問題,項目中涉及到登陸界面鲁驶,由于這個等于是有有效期的鉴裹,隨意你在任何一個界面的觸發(fā)事件中如果登陸失效了,就需要在當前界面彈出登陸界面钥弯,一般都是使用模態(tài)推出效果彈出登陸界面径荔,但是這會涉及到一個很蛋疼的問題,比如你不知到用哪個導航推出合適脆霎,當然是由TabBarController 初始化四個導航控制器的時候所以這個時候手動創(chuàng)建另外一個UIWindow就方便多了总处,廢話不說直接上代碼
.h文件如下
@interface JYSharedLoginView : UIWindow
+(instancetype)sharedInstance;
-(void)showLoginView;
@end
.m文件如下
@implementation JYSharedLoginView
+(instancetype)sharedInstance{
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
});
return sharedInstance;
}
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight)];
if (self) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
label.backgroundColor = [UIColor redColor];
[self addSubview:label];
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(0, 100, 100, 100);
button.backgroundColor = [UIColor blueColor];
[self addSubview:button];
[button addTarget:self action:@selector(butonAction) forControlEvents:(UIControlEventTouchUpInside)];
}
return self;
}
-(void)butonAction{
[UIView animateWithDuration:1.0 animations:^{
self.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
} completion:^(BOOL finished) {
[self resignFirstResponder];
self.hidden = YES;
}];
}
-(void)showLoginView{
[self makeKeyWindow];
self.hidden = NO;
[UIView animateWithDuration:1.0 animations:^{
self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
} completion:^(BOOL finished) {
}];
}