前言
大家都知道在ios8中引入了uialertcontroller,通过uialertcontroller可以方便的添加文本框进行编辑,但是,在输入错误的内容时,如何对用户进行提醒就成了问题,因为uialertcontroller中的所有uialertaction都会导致uialertcontroller的消失。这里,我就描述两种提示的方法,分别是晃动文本框和修改边框的颜色。下面话不多说了,来一起看看详细的实现方法吧。
晃动uitextfield
晃动uitextfield其实就是对它添加一个动画效果,参考了stack overflow上的做法,通过添加position的动画,可以实现uialertcontroller中的uitextfield的晃动效果。
1
2
3
4
5
6
7
8
9
|
- ( void )shakefield:(uitextfield *)textfield {
cabasicanimation *animation = [cabasicanimation animationwithkeypath:@ "position" ];
animation.duration = 0.07;
animation.repeatcount = 4;
animation.autoreverses = yes;
animation.fromvalue = [nsvalue valuewithcgpoint:cgpointmake(textfield.centerx - 10, textfield.centery)];
animation.tovalue = [nsvalue valuewithcgpoint:cgpointmake(textfield.centerx + 10, textfield.centery)];
[textfield.layer addanimation:animation forkey:@ "position" ];
}
|
修改uitextfield的边框颜色
uialertcontroller中文本框的默认边框颜色都是黑色,通常在输入异常时会改为红色进行提醒,这个时候,如果直接修改uitextfield的border将会变成下图样式:
1
2
3
4
5
6
7
8
9
|
- ( void )testalert {
uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@ "测试" message:@ "测试输入框边框颜色" preferredstyle:uialertcontrollerstylealert];
[alert addaction:[uialertaction actionwithtitle:@ "取消" style:uialertactionstylecancel handler:nil]];
[alert addtextfieldwithconfigurationhandler:^(uitextfield * _nonnull textfield) {
textfield.layer.bordercolor = [uicolor redcolor].cgcolor;
textfield.layer.borderwidth = 1;
}];
[self presentviewcontroller:alert animated:yes completion:nil];
}
|
而在实际中我们应该这样修改:
1
2
3
4
5
6
7
8
9
10
|
- ( void )testalert {
uialertcontroller *alert = [uialertcontroller alertcontrollerwithtitle:@ "测试" message:@ "测试输入框边框颜色" preferredstyle:uialertcontrollerstylealert];
[alert addaction:[uialertaction actionwithtitle:@ "取消" style:uialertactionstylecancel handler:nil]];
[alert addtextfieldwithconfigurationhandler:^(uitextfield * _nonnull textfield) {
self.currentfield = textfield;
}];
[self presentviewcontroller:alert animated:yes completion:^{
[[self.currentfield superview] superview].backgroundcolor = [uicolor redcolor];
}];
}
|
这样的产生效果才是我们想要的。
需要注意的是:一定要在present以后进行设置,否则会发现设置是无效的,因为没有present之前,textfield的superview是nil,设置是无效的。
总结
以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
相关文章
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 49
-
2025-05-25 31
-
详解Spring Cloud Stream使用延迟消息实现定时任务(RabbitMQ)
2025-05-29 43 -
使用自助建站微信小程序时,怎样选择合适的模板以匹配品牌形象?
2025-06-04 78 -
2025-05-29 88