键盘在UItextField / UITextView iPhone中向上移动时滚动视图问题

时间:2012-10-19 12:45:12

标签: iphone objective-c uiscrollview uitextfield uitextview

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

的UIView

  Scroll View

          1.ImageView
          2.Table view
          3.Text View

enter image description here

√√当键盘出现/解除时移动滚动视图我在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];
}

√√此方法有助于通过respcet向键盘上下移动视图,

但这是滚动的问题。

用户无法滚动键盘的presensec中的所有视图。视图如下滚动到某个位置,我们无法看到表格的图片/第一列。

enter image description here

如果用户想要在键盘存在的情况下显示第一列/配置文件pic是不可能的。如何解决这个问题。

4 个答案:

答案 0 :(得分: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]; 
}

答案 1 :(得分: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;
    }];
}

答案 2 :(得分:0)

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

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

隐藏键盘通话后。

[scrollView setContentOffset:CGPointZero animated:YES];

答案 3 :(得分:0)

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

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

{{1}}

我希望它对你有所帮助。