iOS 收起键盘的几种方式

时间:2023-02-09 15:25:23

iOS 收起键盘的几种方式

1.一般的view上收起键盘

// 手势

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

[self.cellphone resignFirstResponder];

[self.password resignFirstResponder];

}

2.tableView 上收起键盘

// 通过滚动收起

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

[tf resignFirstResponder];

[self.name resignFirstResponder];

}

// 通过手势

- (void)viewDidLoad {

[super viewDidLoad];

  UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentTableViewTouchInSide)];

tableViewGesture.numberOfTapsRequired = 1;

tableViewGesture.cancelsTouchesInView = NO;

[self.tableView addGestureRecognizer:tableViewGesture];

}

- (void)commentTableViewTouchInSide{

[tf resignFirstResponder];

[self.name resignFirstResponder];

}