当键盘出现时,如何使滚动视图在缩小的视图区域中滚动



我已经从xib将所有视图嵌入到UIScrollView中。滚动视图内容覆盖状态栏下方的所有屏幕。现在,当点击文本字段时,我可以将滚动视图向上移动一点。但我希望它能完全滚动,直到键盘上方也能看到最底部的视图。此外,当滚动视图滚动到顶部时,它应该会到达正常的原始位置。因此,总的来说,我想要一个完全可滚动的功能,就像上面提到的那样,用于我的滚动视图。

我已经完成了以下技巧,但没有运气:

技巧1:更改滚动视图的高度,使内容大于滚动视图的深度,从而使视图可滚动:

-(void)keyboardWillAppear:(NSNotification *)sender
{
CGFloat y_offset=0;
if([UIScreen mainScreen].bounds.size.height == 480){
    y_offset = 80;
} else {
    y_offset = 70;
}
NSDictionary* userInfo = [sender userInfo];
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
keyboardHeight = keyboardEndFrame.size.height;
[UIView animateWithDuration:0.5f animations:^{
    [self.view setFrame:CGRectMake(0, - y_offset, self.view.frame.size.width, self.view.frame.size.height)];
}];

[self.loginScrollView setFrame:CGRectMake(self.loginScrollView.frame.origin.x, self.loginScrollView.frame.origin.y, self.loginScrollView.frame.size.width, [UIScreen mainScreen].bounds.size.height - keyboardHeight)];
}
-(void)keyboardWillDisappear:(NSNotification *)sender
{
[UIView animateWithDuration:0.5f animations:^{
    [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}];
[self.loginScrollView setFrame:CGRectMake(self.loginScrollView.frame.origin.x, self.loginScrollView.frame.origin.y, self.loginScrollView.frame.size.width, [UIScreen mainScreen].bounds.size.height)];
}

技巧2:根据其他建议,我更改了UIScrollView的内容插图。

在键盘WillAppear方法中,我添加了以下代码:

  CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+100, 0.0);
  self.loginScrollView.contentInset = contentInsets;
  self.loginScrollView.scrollIndicatorInsets = contentInsets;

在keyboardWillDisappear方法中,我将contentInset设置回零值。

因此,让我知道是否需要任何其他方式来解决这个问题,或者我需要在滚动视图框架中进行任何其他可能的更改。此外,如果我打开"弹跳垂直"功能,即使屏幕上可以看到我不想要的完整子视图,它也可以弹跳。所以基本上,我希望它在键盘不在的时候冻结,当它打开的时候可以滚动到可视区域。因此,给我其他建议吗?提前谢谢。

从概念角度来看,当"scrollView Size=scrollView ContentSize"时,它不会滚动。为了使其可滚动,我们需要增加contentSize。在您的问题中,您需要调整scrollView的contentSize以及框架。这可以在你的第一种方法中完成。

至于第二种方法,更改边缘镶嵌将为内容可绘制区域创建一种填充。这可以使底部内容可见,但不会影响contentSize,因此视图将不可滚动。

-(void)textFieldDidBeginEdition:(UITextField*)textField

[self animateTextField:textField up:YES];

}

-(void)textFieldDidEndEdition:(UITextField*)textField

[self animateTextField:textField up:NO];

}

-(void)animateTextField:(UITextField*)textField向上:(BOOL)向上{

    const int movementDistance = -60; // change this size if you need
    const float movementDuration = 0.3f; // change this size if you need
    int movement = (up ? movementDistance : -movementDistance);
    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

它对我来说很有用

我真的可以推荐这个库:https://github.com/michaeltyson/TPKeyboardAvoiding

它非常非常容易使用,适用于ScrollView、TableView和CollectionView!

最新更新