模仿安卓toast的AlertView

为了和安卓版提示统一,写了一个简单的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];

}

最后完美的展示了。

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,579评论 1 14
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 855评论 0 0
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,533评论 2 4
  • 望京是北京中心城东北部相对独立、自成一体的综合性新城区,隶属朝阳区,面积为十六平方公里,大量的日韩籍人士聚集于此。...
    一霏姐姐学写作阅读 1,060评论 1 2
  • 【姓名】老马 【住址住址】北京海淀区 【年龄】24 【职业】待业 【兴趣】养花 【婚姻】单身 【性格】内向 【个人...
    娅婷阅读 116评论 0 0