2011-04-01 105 views
0

我想在用户开始编辑UITextField并且文本字段被键盘隐藏时进行UIScrollView滚动。我正在使用来自以下线程的示例。编辑时无法正常工作时滚动视图

How to make a UITextField move up when keyboard is present

我有我的四个视UITextFields。当第一次显示键盘时,视图不会自动滚动。如果我用显示的键盘单击另一个文本字段,则UIScrollView按照预期滚动。 隐藏键盘(通过点击“完成”按钮)并再次轻敲UITextField会出现同样的问题:UIScrollView首先不滚动,但当焦点转换到另一个文本字段时,它完全滚动。

任何人都可以帮我吗?

viewDidLoad我设置的滚动视图

keyboardIsShown = NO; 
CGSize scrollContentSize = CGSizeMake(320, 350); 
self.scrollView.contentSize = scrollContentSize; 

我为键盘通知登录尺寸在viewWillAppear

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window]; 

然后我注销在viewWillDisappear

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 

下面的两个方法由通知调用离子。

- (void)keyboardWillShow:(NSNotification *)n { 
    if (keyboardIsShown) { 
     return; 
    } 

    NSDictionary *userInfo = [n userInfo]; 

    NSValue *boundsValue = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGSize keyboardSize = [boundsValue CGRectValue].size; 

    CGRect viewFrame = self.scrollView.frame; 
    viewFrame.size.height -= (keyboardSize.height - 50); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [UIView setAnimationDuration:0.3]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    keyboardIsShown = YES; 
} 

- (void)keyboardWillHide:(NSNotification *)n { 
    NSDictionary *userInfo = [n userInfo]; 

    NSValue *boundsValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGSize keyboardSize = [boundsValue CGRectValue].size; 

    CGRect viewFrame = self.scrollView.frame; 
    viewFrame.size.height += (keyboardSize.height - 50); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [UIView setAnimationDuration:0.3]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    keyboardIsShown = NO; 
} 
+0

嘿,如果你想在你的“keyboardWillHide:”方法中获得键盘高度,键盘的高度是0吗?你是否在你的方法中设置了一个断点,以查看程序是否进入该方法? – Lepidopteron 2011-04-01 08:53:44

+0

“keyboardWillHide”中键盘的高度为216.当键盘第一次出现时调用'keyboardWillShow',当键盘解散时正确调用'keyboardWillHide'。 – simonbs 2011-04-01 09:00:58

回答

1

如果您想在键盘可见时显示textfeild,请使用下面的代码。不要跟随滚动视图。如果强制使用scrollView,则忽略此答案。



#define kOFFSET_FOR_KEYBOARD 280.0 

- (void)keyboardWillHide:(NSNotification *)notif { 
    [self setViewMoveUp:NO]; 
} 


- (void)keyboardWillShow:(NSNotification *)notif{ 
    [self setViewMoveUp:YES]; 
} 


- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    stayup = YES; 
    [self setViewMoveUp:YES]; 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField { 
    stayup = NO; 
    [self setViewMoveUp:NO]; 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMoveUp:(BOOL)moveUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    CGRect rect = self.view.frame; 
    if (moveUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 

     if (rect.origin.y == 0) { 
      rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
      //rect.size.height += kOFFSET_FOR_KEYBOARD; 
     } 

    } 
    else 
    { 
     if (stayup == NO) { 
      rect.origin.y += kOFFSET_FOR_KEYBOARD; 
      //rect.size.height -= kOFFSET_FOR_KEYBOARD; 
     } 
    } 
    self.view.frame = rect; 
    [UIView commitAnimations]; 
} 

试试这种方法。根据您的要求编辑它。

+0

这是非常优雅的,并为我的特殊情况工作,但不应该调整它滚动到哪里取决于UITextField在视图上的位置?但是,谢谢。这太好了:-) – simonbs 2011-04-01 10:24:46

+0

但它不应该调整它滚动的位置,取决于UITextField在视图上的位置?回复: - 您应该将#define kOFFSET_FOR_KEYBOARD 280.0更改为变量并通过这些方法使用该值进行播放。 – 2011-04-01 11:47:52

+0

我解决了这个问题,通过在' - (void)textFieldDidBeginEditing:(UITextField *)textField'中保存选定的文本字段为'textFieldSelected',然后用'rect.origin.y - = textFieldSelected.frame.origin.y替换'kOFFSET_FOR_KEYBOARD' - kKeyboardOffset;'我的新'kKeyboardOffset'是距离视图顶部的距离。非常感谢你! – simonbs 2011-04-01 13:14:53