【问题标题】:Hiding keyboard with UIScrollView without glitches使用 UIScrollView 隐藏键盘而不会出现故障
【发布时间】:2011-02-26 22:48:50
【问题描述】:

我有多个可编辑的文本文件,其中一些被键盘覆盖。所以我用了 UIScrollView,效果很好。

问题是我想隐藏键盘。如果我向下滚动,在键盘隐藏后,一切都会像开始时一样跳起来(没有键盘)。我想在键盘隐藏时对这部分进行补间。
到目前为止,我得到了这段代码(键盘事件的 2 种方法):

-(void)keyboardWillShow:(NSNotification *)notif{
    if(keyboardVisible)
        return;
    keyboardVisible = YES;

    NSDictionary* info = [notif userInfo];
    NSValue* value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    CGRect viewFrame = self.view.frame;
    viewFrame.size.height -= keyboardSize.height;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3];
    [scrollView setFrame:viewFrame];
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notif{
    if(!keyboardVisible)
        return;

    keyboardVisible = NO;
    NSDictionary* info = [notif userInfo];
    NSValue* value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    CGRect viewFrame = self.view.frame;
    viewFrame.size.height += keyboardSize.height;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3];
    [scrollView setFrame:viewFrame];
    [UIView commitAnimations];
}

隐藏键盘效果很好,但不幸的是当用户从一个文本字段切换到另一个文本字段时它不起作用。它将一个接一个地触发keyboardWillHide 和keyboardWillShow 事件。这将产生两个动画,第二个会打断第一个。看起来不太好。

问题是即使键盘不会隐藏,keyboardWillHide 也会触发。那时我不知道键盘是否会再次显示。

我也尝试过使用 UIScrollView scrollRectToVisible 和 setContentOffset 方法。但是当键盘隐藏时它们会导致故障。

【问题讨论】:

    标签: iphone objective-c keyboard uiscrollview


    【解决方案1】:

    使用此方法处理多个文本字段和键盘

    -(void)scrollViewToCenterOfScreen:(UIView *)theView 
    { 
        CGFloat viewCenterY = theView.center.y; 
        CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
    
        CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard  
    
        CGFloat y = viewCenterY - availableHeight / 2.0; 
        if (y < 0) { 
            y = 0; 
        } 
        [scrollview setContentOffset:CGPointMake(0, y) animated:YES]; 
    
    }
    
    
    call it in textfield delegate=
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        [self scrollViewToCenterOfScreen:textField]; 
    }
    
    and set scroll view frame in the below textfield delegate=
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        [self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
        return YES;
    }
    

    【讨论】:

      【解决方案2】:

      为什么不使用布尔值来表示是外观还是变化?

      【讨论】:

      • 什么意思?我正在检查键盘是否不止一次显示/隐藏。但是在键盘可见时,如果用户单击不同的文本字段,它将触发隐藏事件,然后触发显示事件......在隐藏事件期间,我无法确定是否会有显示事件。这就是问题
      • keyboardDidShow 上设置布尔值 1,在 keyboardDidHide 上设置 0 - 只有在事件之前键盘不可见时才会触发。
      • 问题是即使用户触摸不同的文本字段也会触发keyboardDidHide。但是键盘不会隐藏,只会触发隐藏事件和显示事件。
      猜你喜欢
      • 2012-05-18
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 2020-10-12
      相关资源
      最近更新 更多