iOS类似Android上toast效果
2016-01-07 20:37
555 查看
做过Android开发的人都知道toast,它会在界面上显示一排黑色背景的文字,用于提示用户信息。但iOS上并没有类似的控件,so,自己写一个吧。
原理:
说白了,Android中的toast可以理解成iOS中的一个黑色背景的UILabel。。。
效果图:
是不是还可以,什么背景颜色,字体大小,位置,统统都是可以自己设置的。
![](https://img-blog.csdn.net/20160107202942373?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160107202957485?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码:
//尺寸设置
#define aiScreenWidth [UIScreen mainScreen].bounds.size.width
#define aiScreenHeight [UIScreen mainScreen].bounds.size.height
#define STATUS_BAR_HEIGHT [[UIApplication sharedApplication] statusBarFrame].size.height
#define NAVIGATION_BAR_HEIGHT self.navigationController.navigationBar.frame.size.height
#define TAB_BAR_HEIGHT self.tabBarController.tabBar.frame.size.height
使用方法:
原理:
说白了,Android中的toast可以理解成iOS中的一个黑色背景的UILabel。。。
效果图:
是不是还可以,什么背景颜色,字体大小,位置,统统都是可以自己设置的。
代码:
//尺寸设置
#define aiScreenWidth [UIScreen mainScreen].bounds.size.width
#define aiScreenHeight [UIScreen mainScreen].bounds.size.height
#define STATUS_BAR_HEIGHT [[UIApplication sharedApplication] statusBarFrame].size.height
#define NAVIGATION_BAR_HEIGHT self.navigationController.navigationBar.frame.size.height
#define TAB_BAR_HEIGHT self.tabBarController.tabBar.frame.size.height
- (void) addToastWithString:(NSString *)string inView:(UIView *)view { CGRect initRect = CGRectMake(0, STATUS_BAR_HEIGHT + 44, aiScreenWidth, 0); CGRect rect = CGRectMake(0, STATUS_BAR_HEIGHT + 44, aiScreenWidth, 22); UILabel* label = [[UILabel alloc] initWithFrame:initRect]; label.text = string; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.font = [UIFont systemFontOfSize:14]; label.backgroundColor = [UIColor colorWithRed:0 green:0.6 blue:0.9 alpha:0.6]; [view addSubview:label]; //弹出label [UIView animateWithDuration:0.5 animations:^{ label.frame = rect; } completion:^ (BOOL finished){ //弹出后持续1s [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(removeToastWithView:) userInfo:label repeats:NO]; }]; } - (void) removeToastWithView:(NSTimer *)timer { UILabel* label = [timer userInfo]; CGRect initRect = CGRectMake(0, STATUS_BAR_HEIGHT + 44, aiScreenWidth, 0); // label消失 [UIView animateWithDuration:0.5 animations:^{ label.frame = initRect; } completion:^(BOOL finished){ [label removeFromSuperview]; }]; }
使用方法:
[self addToastWithString:@"更新到最新数据啦~" inView:self.view];
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- android自定义toast(widget开发)示例
- Android开发中4个常用的工具类【Toast、SharedPreferences、网络及屏幕操作】
- 探讨Android与iOS,我们将何去何从?
- Android控件系列之Toast使用介绍
- Android编程之Button控件配合Toast控件用法分析
- Android中使用Toast.cancel()方法优化toast内容显示的解决方法
- Toast和Handler的间隔使用实例