【问题标题】:How to make the scrollview scrollable in reduced view area when keyboard appears键盘出现时如何使滚动视图在缩小的视图区域中可滚动
【发布时间】:2014-11-10 03:56:39
【问题描述】:

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

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

技巧 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 的 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 功能,即使我不想要的完整子视图在屏幕上可见,它也能够反弹。所以基本上我希望它在键盘不存在时冻结,并且在它启动时可以滚动到可见区域。因此,给我任何其他建议吗?提前致谢。

【问题讨论】:

  • 在您的第一种方法中,将滚动视图 contentSize 设置为等于您的全屏尺寸(不存在键盘)以及框架调整以查看您已经执行的操作。跨度>
  • 是的,这就像魅力!谢谢@甘道夫。请将其发布为答案,以便每个人都知道解决方案,我将接受相同的解决方案。 :)
  • 很高兴知道它有帮助。我已经发布了答案。你可以接受。 :)

标签: ios objective-c uiscrollview keyboard frame


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    对我有用

    【讨论】:

    • 我可以向上移动视图。但我希望滚动视图可以完全滚动。
    【解决方案3】:

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

    非常好用,适用于ScrollView、TableView和CollectionView!

    【讨论】:

    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 2012-06-29
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多