【问题标题】:Scroll view issues when key board moves up in UItextField / TextView iPhone键盘在 UItextField / UITextView iPhone 中向上移动时的滚动视图问题
【发布时间】:2012-10-10 02:06:01
【问题描述】:

我的视图上有一个 scrollview,因为我拥有所有子视图,如下所示

界面视图

  Scroll View

          1.ImageView
          2.Table view
          3.Text View

√√ 要在键盘出现/关闭时移动滚动视图,我在 textview 委托方法中实现了如下逻辑 √√

- (void)textViewDidBeginEditing:(UITextView *)textView;
{
      //To move the view down 
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDelegate:self];
      [UIView setAnimationDuration:0.3];
      [UIView setAnimationBeginsFromCurrentState:YES];
      scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y - 120), scrollView.frame.size.width, scrollView.frame.size.height);
      [UIView commitAnimations];

}


- (void)textViewDidEndEditing:(UITextView *)textView 
{   
      //To move the view down 
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDelegate:self];
      [UIView setAnimationDuration:0.3];
      [UIView setAnimationBeginsFromCurrentState:YES];
      scrollView.frame = CGRectMake(scrollView.frame.origin.x, (scrollView.frame.origin.y + 120), scrollView.frame.size.width, scrollView.frame.size.height);
      [UIView commitAnimations];
}

√√ 此方法有助于根据键盘上下移动视图,

但是这里是滚动的问题。

用户不能在键盘上滚动所有视图。视图向上滚动到某个位置如下,我们看不到图片/表格的第一列。

如果用户想在有键盘的情况下显示第一列/个人资料图片是不可能的。如何解决这个问题。

【问题讨论】:

    标签: iphone objective-c uiscrollview uitextfield uitextview


    【解决方案1】:

    先设置滚动内容如

    scrollView.contentSize=CGSizeMake(320, 600);
    

    然后在调用 textView 委托方法时为内容设置动画

    - (void)textViewDidBeginEditing:(UITextView *)textView;
    {  
      [scrollView setContentOffset:CGPointMake(0, 120 ) animated:YES]; 
    }
    
    - (void)textViewDidEndEditing:(UITextView *)textView 
    {
     [scrollView setContentOffset:CGPointMake(0, 0 ) animated:YES]; 
    }
    

    【讨论】:

      【解决方案2】:

      首先,您不想使用textViewDidBeginEditing: 方法来显示/隐藏键盘。为此,您应该注册相应的通知。这可以放在你的viewDidLoad。

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

      然后,如果显示键盘,请确保不要移动整个滚动视图,而是缩小它的大小。

      - (void)keyboardWillShow:(NSNotification *)notif
      {
          CGSize kbSize = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
          double duration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
      
          // Shrink the scroll view's content
          UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
          self.scrollView.contentInset = contentInsets;
          self.scrollView.scrollIndicatorInsets = contentInsets;
      
          // Scroll the text field to be visible (use own animation with keyboard's duration)
          CGRect textFieldRect = [_activeTextField convertRect:_activeTextField.bounds toView:self.scrollView];
          [UIView animateWithDuration:duration animations:^{
              [self.scrollView scrollRectToVisible:CGRectInset(textFieldRect, 0.0, -10.0) animated:NO];
          }];
      }
      

      此代码具有一些附加功能,因为我还必须移动滚动视图的内容,但可以帮助您入门。那么当键盘隐藏时...

      - (void)keyboardWillHide:(NSNotification *)notif
      {
          double duration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
      
          // Set content inset back to default with a nice little animation
          [UIView animateWithDuration:duration animations:^{
              UIEdgeInsets contentInsets = UIEdgeInsetsZero;
              self.scrollView.contentInset = contentInsets;
              self.scrollView.scrollIndicatorInsets = contentInsets;
          }];
      }
      

      【讨论】:

        【解决方案3】:

        显示键盘时应用内容偏移。并在隐藏时恢复。

        [scrollView setContentOffset:CGPointMake(0.0f, 120.0f) animated:YES];
        

        隐藏键盘调用后。

        [scrollView setContentOffset:CGPointZero animated:YES];
        

        【讨论】:

          【解决方案4】:

          只需更改 ScrollView 的 ContentOffset 无需像您在代码中所做的那样更改 ScrollView 的原点,这样滚动视图将始终滚动。 这样做你需要设置 upur ScrollViw 的 ContentSize 至少 550 只检查什么是适当的 ContentSize。我创建了一个方法,您只需要调用我的方法并适当地传递所需的参数

          还有一件事设置ContentSIze 的UISCrollView 超过它的高度SuperView at where you have created theScrollViewor inViewViewAppearmethod. (CurrentView)

            [contentScrollView setContentSize:CGSizeMake(320, 620)];
          
            And Call This Method As you click on TextField or on the Screen(In Touch Ended when you need to readjust ContentOffset)
          
            - (void)adjustContetOffset:(BOOL)yes withOffSet:(CGFloat)offSet
            {  
          
              contentOffSetY = offSet ;
               //By this you can track more suppose you have change the Orientation of Device then set Appropriate Offset.
              [UIView beginAnimations:nil context:nil];
              [UIView setAnimationDuration:0.4];
          
              if(yes){
                  isScrolledUp = TRUE;
                  contentScrollView.contentOffset = CGPointMake(0, contentOffSetY);
              }
              else
              {
                  isScrolledUp = FALSE;
                 //by this flag you can Track that OffSet of ScrollView has changed 
                  contentScrollView.contentOffset = CGPointMake(0, contentOffSetY);
              }
          
              [UIView commitAnimations];
          
            }
          
              //this methods 'll call whenever you 'll touch the SCreen
            -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
          
          
              if(isScrolledUp){
              [self adjustContetOffset:NO withOffSet: 0.0];
             }
              [searchTextField resignFirstResponder];
             }
          

          希望对你有帮助。

          【讨论】:

            猜你喜欢
            • 2015-10-19
            • 1970-01-01
            • 1970-01-01
            • 2017-05-27
            • 2021-11-30
            • 1970-01-01
            • 2015-03-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多