ios中隱藏鍵盤的方式

1. 重寫view的 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 方法


2. 爲view添加tapGesture

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[view addGestureRecognizer:tapGesture];

- (void)hideKeyboard
{
    [self.view endEditing:NO];
}

假如view是tableview或者collectionView,如果不設置

tapGesture.cancelsTouchesInView = NO;
那麼原來的cell將變得不能點擊。


[self.view endEditing: NO]的解釋

Apple Doc:

"This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."


3. 可以設置一個透明的大的button響應事件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章