关于ios:当键盘出现时,如何使缩小视图区域中的scrollview可滚动

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

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

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

技巧1:更改scrollview的高度,使内容超过scrollview高度,因此视图可滚动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-(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方法中,我添加了以下代码:

1
2
3
4
  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框架中进行任何其他可能的更改。此外,如果我打开弹跳功能,即使屏幕上显示我不想要的完整子视图,它也能够反弹。所以基本上我希望它在键盘不存在时冻结,并且当它可用时可滚动到可视区域。那么,给我任何其他建议?提前致谢。


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

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


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

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


- (void)textFieldDidBeginEditing:(UITextField *)textField

{

1
[self animateTextField:textField up:YES];

}

- (void)textFieldDidEndEditing:(UITextField *)textField

{

1
[self animateTextField:textField up:NO];

}

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

1
2
3
4
5
6
7
8
9
10
11
    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];
}

它对我有用