我一直在寻找各地......也许我没有使用正确的搜索词,因为我相信这是一个常见的问题。
当用户通过单击按钮降低键盘来解除键盘时,是否有可以处理的事件。
当uitextfield成为第一响应者时移动一个视图但我想在点击此按钮时再将其移动
答案 0 :(得分:1)
尝试使用通知。将其添加到您的viewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
然后创建一个名为keyboardWillHide
的方法:
- (void)keyboardWillHide:(NSNotification *)notification {
//do whatever you need
}
希望有所帮助
答案 1 :(得分:0)
查看“管理键盘”部分中的第二段:http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITextField_Class/Reference/UITextField.html
答案 2 :(得分:0)
通过使用NSNotificationCenter,您可以获得键盘事件。您可以在viewWillAppear中注册键盘事件,并且不要忘记在viewWillDisapper中取消注册。
我们将在这里使用两个通知:
UIKeyboardDidHideNotification apple docs
你可以这样做:
//注册活动
[[NSNotificationCenter defaultCenter]
的addObserver:自
选择器:@selector(keyboardDidShow :)
名称:UIKeyboardDidShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]
的addObserver:自
selector:@selector(keyboardDidHide :)
名称:UIKeyboardDidHideNotification
对象:无];
//设置内容大小 scrollview.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT); //例如(320460)
//最初隐藏键盘 keyboardVisible = NO; //在.h声明BOOL keyboardVisible; }
- (void)viewWillDisappear:(BOOL)动画{
NSLog(@“取消注册键盘事件”);
[[NSNotificationCenter defaultCenter]
removeObserver:自];
}
- (void)keyboardDidShow:(NSNotification *)notif {
NSLog(@“键盘可见”);
//如果键盘可见,请返回
if(keyboardVisible){
NSLog(@“键盘已经可见。忽略通知。”);
返回;
}
// 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;
}
- (void)keyboardDidHide:(NSNotification *)notif {
//键盘是否已经显示
if(!keyboardVisible){
NSLog(@“键盘已隐藏。忽略通知。”);
返回;
}
// 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
{
[textField resignFirstResponder];
返回YES;
}
希望能帮到你:)。