1

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

界面视图

  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];
}

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

但这是滚动的问题。

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

在此处输入图像描述

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

4

4 回答 4

3

首先将滚动内容设置为

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]; 
}
于 2012-10-19T12:58:56.840 回答
1

首先,您不想使用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;
    }];
}
于 2012-10-19T12:59:25.210 回答
0

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

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

隐藏键盘调用后。

[scrollView setContentOffset:CGPointZero animated:YES];
于 2012-10-19T12:55:27.527 回答
0

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

还有一件事设置的ContentSIze超过UISCrollView它的SuperView at where you have created theScrollView or inViewViewAppear的高度method. (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];
   }

我希望它对你有帮助。

于 2012-10-19T13:01:46.123 回答