关于UIWindow的使用

关于手动创建UIWindow的使用,最近做项目遇到一个很是头疼的问题,项目中涉及到登陆界面,由于这个等于是有有效期的,随意你在任何一个界面的触发事件中如果登陆失效了,就需要在当前界面弹出登陆界面,一般都是使用模态推出效果弹出登陆界面,但是这会涉及到一个很蛋疼的问题,比如你不知到用哪个导航推出合适,当然是由TabBarController 初始化四个导航控制器的时候所以这个时候手动创建另外一个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) {
    
    }];
}

你可能感兴趣的:(关于UIWindow的使用)