oc UITextField隐藏键盘

创建一个UITextField,弹出键盘,但会遇到一个问题,键盘会挡住界面或者希望当点击其他地方时键盘收起。

参考官方文档:(文档中有部分代码解释)https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

方法一:点击键盘外的地方收起键盘

情形A:在想要收起键盘的地方(一般是回车键,或 键盘tabbar的“确认”键)

输入 下面的代码:

[myTextField resignFirstResponder];   //myTextField  创建的UITextField的名称

(意思是使myTextField失去中心)

情形B:点击屏幕空白处,收起键盘

重写方法

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

[myTextField resignFirstResponder]; // 空白处收起

}

情形c:多个UI,画面叠加(比如弹出的pickview,image等)

先个UI设置tag,系统withTag找到这个UI,在调用UI的地方

[myTextField resignFirstResponder]; // 先收起TextField

方法二:键盘弹出时,整个界面往上推,输入完成后,收起键盘(更好)

顾名思义,就是在键盘弹出时,整个界面往上推送,重新设置contentoffset。适合在TextField在页面下方,可能被键盘遮挡的情况。

你可能感兴趣的:(oc UITextField隐藏键盘)