我一直在寻找无处不在...也许我没有使用正确的搜索词,因为我确实相信这是一个常见问题。通过点击键盘按钮解雇屏幕键盘的事件
当用户通过单击按钮放下键盘而解散键盘时,是否可以处理该事件。
我移动视图了为UITextField变得firstresponder,但我想再次向下移动时,该按钮被窃听
我一直在寻找无处不在...也许我没有使用正确的搜索词,因为我确实相信这是一个常见问题。通过点击键盘按钮解雇屏幕键盘的事件
当用户通过单击按钮放下键盘而解散键盘时,是否可以处理该事件。
我移动视图了为UITextField变得firstresponder,但我想再次向下移动时,该按钮被窃听
尝试使用通知。即添加到您的viewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
,然后创建一个名为keyboardWillHide
方法:
- (void)keyboardWillHide:(NSNotification *)notification {
//do whatever you need
}
希望它可以帮助
使用NSNotificationCenter你键盘events.You可以在键盘事件寄存器viewWillAppear并且不要忘记在viewWillDisapper中取消注册。
我们将在这里使用两个通知:
UIKeyboardDidHideNotification apple docs
你可以做一些这样的事:
//注册事件
[[NSNotificationCenter defaultCenter] 的addObserver:自 选择器:@selector(keyboardDidShow :) 名:UIKeyboardDidShowNotification 对象:无];
[[NSNotificationCenter defaultCenter] 的addObserver:自 选择器:@selector(keyboardDidHide :) 名:UIKeyboardDidHideNotification 对象:无];
//设置内容大小 scrollview.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT); //例如, (320,460)
//最初键盘被隐藏 keyboardVisible = NO; // in。h声明BOOL keyboardVisible; }
- (无效)viewWillDisappear:(BOOL)动画{
的NSLog(@ “取消注册键盘事件”);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (无效)keyboardDidShow:(NSNotification *)通知符 {
的NSLog(@ “键盘是可见的”);
//如果键盘可见,则返回
if(keyboardVisible)NSLog(@“Keyboard is already visible。Ignore notification。”);
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = scrollview.contentOffset; //in .h declare CGPoint offset and UIScrollView *scrollview.;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = scrollview.frame;
viewFrame.size.height -= keyboardSize.height;
scrollview.frame = viewFrame;
CGRect textFieldRect = [activeField frame];//in .h UITextField *activeField;
textFieldRect.origin.y += 10;
[scrollview scrollRectToVisible:textFieldRect animated:YES];
// Keyboard is now visible
keyboardVisible = YES;
}
- (空)keyboardDidHide:(!keyboardVisible)(NSNotification *)通知符 {
//已经出现该键盘
如果{
的NSLog( @“键盘已隐藏,忽略通知。”);
return;
}
// Reset the frame scroll view to its original value
scrollview.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);
// Reset the scrollview to previous location
scrollview.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}
- (BOOL)textFieldShouldReturn:(的UITextField *)的TextField
{
[文本字段resignFirstResponder];
返回YES;
}
希望能帮到你:)