美文网首页
模仿安卓toast的AlertView

模仿安卓toast的AlertView

作者: ___Lynn | 来源:发表于2017-01-05 18:12 被阅读0次

    为了和安卓版提示统一,写了一个简单的toast页面,下面上代码

    创建一个UIView,有俩属性UILable和NSTimer

    @property (nonatomic, strong) UILabel *label;

    @property (nonatomic, strong) NSTimer *timer;

    一个获得当前ViewController的+号方法


    +(UIViewController *)getCurrentVC{

    UIViewController *result = nil;

    UIWindow * window = [[UIApplication sharedApplication] keyWindow];//获得当前window

    //如果当前window不是正常的window,找到等级为Normal的window

    if (window.windowLevel != UIWindowLevelNormal){

    NSArray *windows = [[UIApplication sharedApplication] windows];

    for(UIWindow * tmpWin in windows){

    if (tmpWin.windowLevel == UIWindowLevelNormal){

    window = tmpWin;

    break;

    }

    }

    }

    UIView *frontView = [[window subviews] objectAtIndex:0];//获得当前view

    id nextResponder = [frontView nextResponder];

    if ([nextResponder isKindOfClass:[UIViewController class]])

    {

    result = nextResponder;

    if([result isKindOfClass:[UITabBarController class]]){

    UITabBarController *tabVc=(UITabBarController *)result;

    result=tabVc.selectedViewController;

    }

    if([result isKindOfClass:[UINavigationController class]]){

    UINavigationController *tabVc=(UINavigationController *)result;

    result = tabVc.viewControllers.lastObject;//NavigationController控制多个ViewController时 数组里最后的那个就是当前的

    }

    }else{

    result = window.rootViewController;

    }

    return result;

    }

    接下来只要在init方法里获得当前viewController并展示就行了

    这里写了一个设置title的+号方法

    + (void)showWithTitle:(NSString *)title{

    CGSize titleSize = [NSString adaptiveWithText:title fontName:@"system" size:12 rect:CGSizeMake(10000, 20)];

    CGRect frame = CGRectMake((ScreenWidth - titleSize.width - 15) / 2 , ScreenHeight / 3 * 2, titleSize.width + 10, titleSize.height + 10);

    StaffAlertView *alertView = [[StaffAlertView alloc]initWithFrame:frame];

    alertView.backgroundColor = [UIColor blackColor];

    alertView.layer.cornerRadius = CGRectGetHeight(frame) / 4;

    alertView.label = [[UILabel alloc]initWithFrame:CGRectMake(6, 4, titleSize.width, titleSize.height)];

    alertView.label.textColor = [UIColor whiteColor];

    alertView.label.textAlignment = NSTextAlignmentCenter;

    alertView.label.font = [UIFont systemFontOfSize:12];

    alertView.label.text = title;

    [[StaffAlertView getCurrentVC].view addSubview:self];

    [alertView performSelector:@selector(dismiss:) withObject:alertView afterDelay:2.0];

    [alertView addSubview:alertView.label];

    }

    - (void)dismiss:(UIView *)view{

    [view removeFromSuperview];

    }

    写完了测试了下,发现如果有键盘存在的时候会被遮挡住。这时候就需要利用window来展示

    UIWindow *window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    _window.windowLevel = UIWindowLevelAlert;//window等级设为最高

    [window addSubview:alertView];

    [_window makeKeyAndVisible];//展示该window

    - (void)dismiss:(UIView *)view{

    [_window resignKeyWindow];

    }

    再测试下 结果出现提示的时候键盘会回收  提示完了键盘才会弹出来 这是因为我们新建的window 把键盘window替代了。这时候就应该获得当前window并展示。最后更改如下

    UIWindow *window = [[UIApplication sharedApplication] windows].lastObject;

    [window addSubview:alertView];

    [alertView performSelector:@selector(dismiss:) withObject:alertView afterDelay:2.0];

    [alertView addSubview:alertView.label];

    - (void)dismiss:(UIView *)view{

    [view removeFromSuperview];

    }

    最后完美的展示了。

    年尾项目赶工,很久没上。以后慢慢补上,坚持一周一篇,新年加油!

    相关文章

      网友评论

          本文标题:模仿安卓toast的AlertView

          本文链接:https://www.haomeiwen.com/subject/bomzvttx.html