繁体   English   中英

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

[英]How to make the scrollview scrollable in reduced view area when keyboard appears

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

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

技巧1:更改scrollview的高度,以使内容大于scrollview的高度,因此该视图可滚动:

-(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的contentInset。

在keyboardWillAppear方法中,我添加了以下代码:

  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设置回零值。

因此,请告诉我是否需要其他任何方法来解决此问题,或者需要在滚动视图框架中进行任何其他可能的更改。 而且,如果我打开了bouncesVertically功能,即使在屏幕上看到了我不想要的完整子视图,它也可以反弹。 因此,基本上,我希望它在键盘不存在时冻结,并且在其向上滚动时可滚动到可见区域。 因此,还有其他建议吗? 提前致谢。

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

[self animateTextField:textField up:YES];

}

-(void)textFieldDidEndEditing:(UITextField *)textField

{

[self animateTextField:textField up:NO];

}

-(void)animateTextField:(UITextField *)textField up:(BOOL)up {

    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!

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM