当前位置:首页 > 通信资讯 > 正文

ios键盘下方空白(ios键盘空格)

ios7 点击空白处隐藏键盘的几种方法,具体如下:

ios开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们可以实现点击键盘以外的空白区域来将键盘隐藏,以下我总结出了几种隐藏键盘的方法:

首先说明两种可以让键盘隐藏的method:

1、[view endediting:yes] 这个方法可以让整个view取消第一响应者,从而让所有控件的键盘隐藏。

2、[textfiled resignfirstresponder] 这个则是比较常用的让某个textfiled的键盘隐藏。

接下来就是几种实现方式:

第一种: 使用view的touchesbegan:触摸事件来实现对键盘的隐藏,当点击view的区域就会触发这个事件

?
1 2 3 -(void)touchesbegan:(nsset *)touches withevent:(uievent *)event{ [textfiled resignfirstresponder]; }

第二种:创建自定义的触摸手势来实现对键盘的隐藏:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 - (void)viewdidload { [super viewdidload]; uitapgesturerecognizer *tapgesturerecognizer = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(keyboardhide:)]; //设置成no表示当前控件响应后会传播到其他控件上,默认为yes。 tapgesturerecognizer.cancelstouchesinview = no; //将触摸事件添加到当前view [self.view addgesturerecognizer:tapgesturerecognizer]; } -(void)keyboardhide:(uitapgesturerecognizer*)tap{ [textfiled resignfirstresponder]; }

第三种:修改xib中uiview的custom class为uicontrol,uicontrol是一些常用控件如uibutton的父类,是uiview的派生类,实现了对触摸和下按的封装。

1、首先设置xib中得uiview的custom class为uicontrol

ios键盘下方空白(ios键盘空格)

2、设置关系事件,将xib中得uiview拖到.h区中

ios键盘下方空白(ios键盘空格)

设置好事件为touch up inside

3、编写隐藏代码:

?
1 2 3 - (ibaction)touchview:(id)sender { [self.view endediting:yes]; }

好了,以上是三种比较常用的隐藏键盘的方法,每种都可以用于不同的场合和它的利与弊,就看如何运用了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/swingpyzf/article/details/17091567

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。