代码之家  ›  专栏  ›  技术社区  ›  sudheer

当键盘用于滚动视图时,文本字段消失

  •  0
  • sudheer  · 技术社区  · 12 年前

    我在UisrollView中出现键盘问题。

    我添加了一个UIScrollview作为

    scrlView=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 140, 1000, 600)];
    scrlView.scrollEnabled=YES;
    scrlView.showsVerticalScrollIndicator=YES;
    scrlView.bounces=NO;
    

    在这个滚动视图中,我添加了10行UITextFields,每行有5个textFields,每个textfield的高度是50px。 当试图编辑文本字段时,它与键盘重叠。为此,我尝试了以下代码

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    
       - (void)keyboardWasShown:(NSNotification*)aNotification
      {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = selectetTxtfld.superview.frame;
    bkgndRect.size.height += kbSize.height;
    [selectetTxtfld.superview setFrame:bkgndRect];
    [scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y) animated:YES];
    }
    

    }

    //发送UIKeyboardWillHideNotification时调用

     - (void)keyboardWillBeHidden:(NSNotification*)aNotification
      {
      UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    
      [UIView animateWithDuration:0.4 animations:^{
         scrlView.contentInset = contentInsets;
      }];
      scrlView.scrollIndicatorInsets = contentInsets;
     }
    

    但是文本字段没有出现在键盘上。它出现在滚动视图的Y点位置

    帮我解决这个问题。我在StackOverFlow中看到了很多答案。但没有解决我的问题

    2 回复  |  直到 12 年前
        1
  •  1
  •   dRAGONAIR    12 年前
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGFloat offSetAfterKeyboardIsDisplayed = scrlview.contentOffset.y + kbSize.height;
    
    [UIView animateWithDuration:0.3 animations:^{
    //adding content inset at the bottom of the scrollview
       scrlView.contentInset = UIEdgeInsetMake(0,0,kbSize.height,0);
       [scrlview setContentOffset:offSetAfterKeyboardIsDisplayed]
    }];
    }
    
    
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
    
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGFloat offSetAfterKeyboardResigns = scrlview.contentOffset.y - kbSize.height;
    
    [UIView animateWithDuration:0.3 animations:^{
       scrlView.contentInset = UIEdgeInsetsZero;
       [scrlview setContentOffset:offSetAfterKeyboardResigns]
    }];
    }
    
        2
  •  1
  •   dRAGONAIR    12 年前

    在键盘中显示: 1.在滚动视图的底部添加内容插图,其值等于键盘的高度。 2.setContentOffset=当前偏移量+键盘的高度。 注:1&3应该在持续时间等于0.30的动画块中完成

    在键盘中WillBeHidden: 1.设置contentInset=UIEdgeInsetsZero 2.setContentOffset=当前偏移量-键盘的高度。 注:1&3应该在持续时间等于0.30的动画块中完成

    这应该可以解决您的问题:)