我试图在视图中的任何位置关闭键盘。这是我的代码
- (void)registerForNotifcationOfKeyboard
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = activeField.superview.frame;
bkgndRect.size.height += kbSize.height;
[activeField.superview setFrame:bkgndRect];
[scrollView setContentOffset:CGPointMake(0.0,kbSize.height/2 - activeField.frame.origin.y) animated:YES];
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void) textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
-(BOOL) disablesAutomaticKeyboardDismissal
{
return NO;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)viewDidLoad
{
[self registerForNotifcationOfKeyboard];
self.progressBar.hidden = YES;
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[super viewDidLoad];
}
-(void) dismissKeyboard
{
[activeField resignFirstResponder];
}
当我在任何地方按下水龙头时,这个函数dismissKeyboard
确实被调用但它永远不会关闭键盘。
请如果有人有任何想法
最好的祝福