1

我一直在到处寻找......也许我没有使用正确的搜索词,因为我相信这是一个常见问题。

当用户通过单击按钮降低键盘来关闭键盘时,我可以处理一个事件。

红色标记的按钮

当 uitextfield 成为第一响应者时,我将视图向上移动,但是当点击此按钮时,我想再次将其向下移动

4

3 回答 3

1

尝试使用通知。将其添加到您的viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

然后创建一个名为keyboardWillHide

- (void)keyboardWillHide:(NSNotification *)notification {
  //do whatever you need
}

希望能帮助到你

于 2012-05-09T09:37:41.923 回答
0

查看“管理键盘”部分的第二段:http: //developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITextField_Class/Reference/UITextField.html

于 2012-05-09T09:26:33.633 回答
0

通过使用 NSNotificationCenter,您可以获得键盘事件。您可以在 viewWillAppear 中注册键盘事件,并且不要忘记在 viewWillDisapper 中取消注册。
我们将在这里使用两个通知:

  1. UIKeyboardDidShowNotification苹果文档
  2. UIKeyboardDidHideNotification苹果文档

    你可以做这样的事情:

    • (void) viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      NSLog(@"注册键盘事件");

    // 注册事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];

    // 设置内容大小 scrollview.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);//例如 (320,460)

    //最初键盘是隐藏的keyboardVisible = NO;//在.h中声明BOOL keyboardVisible; }

-(void) viewWillDisappear:(BOOL)animated {
NSLog (@"注销键盘事件");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(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];
返回是;
希望
能帮助你:)

于 2012-05-09T11:06:27.760 回答