欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发之各种收起键盘的方法

程序员文章站 2022-05-04 09:22:00
1、用的比较多的方法:点击背景view收起键盘或者直接使用也可以(你的view必须是继承于uicontrol) [self.view endediting:yes]; 2、万能方法:在任何地方都可以...

1、用的比较多的方法:点击背景view收起键盘或者直接使用也可以(你的view必须是继承于uicontrol)

[self.view endediting:yes];

2、万能方法:在任何地方都可以使用这种方法来关闭/收起键盘

[[[uiapplication sharedapplication] keywindow] endediting:yes];

3、点击return按扭时收起键盘

(bool)textfieldshouldreturn:(uitextfield *)textfield { 

return [textfield resignfirstresponder]; }

直接发送 resignfirstresponder 消息

[[uiapplication sharedapplication] 

sendaction:@selector(resignfirstresponder) to:nil from:nil 

forevent:nil];

使用场景:

获取到 uitextfield 对象时,最好使用

[obj resignfirstresponder]

方法; 

有很多个 uitextfield 对象,也可获取到 viewcontroller 的 view 时,可以使用 [[[uiapplication sharedapplication] keywindow] endediting:yes] 方法; 

如果当前 viewcontroller比较难获取,可以使用第2种或第4种方法。 

5.触摸uitableview收起键盘,一般聊天会涉及到

uitapgesturerecognizer *tableviewgesture = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(commenttableviewtouchinside)];
tableviewgesture.numberoftapsrequired = 1;
tableviewgesture.cancelstouchesinview = no;
[self.tableview addgesturerecognizer:tableviewgesture];
- (void)commenttableviewtouchinside{
      [self.view endediting:yes];
      }