简体   繁体   中英

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

I have embedded all my views in a UIScrollView from xib. The scrollview contents cover all screen below status bar. Now when the textfield is tapped, I am able to move the scrollview little up. But I want it to be completely scrollable till the bottom most view is also visible above the keyboard. Also when the scrollview is scrolled till top , it should come to normal original positions. Hence, Overall I want a completely scrollable functionality like mentioned above for my scrollview.

I am done with following tricks but with no luck:

Trick 1: Change the height of the scrollview so that the content is more than scrollview height and hence the view is scrollable:

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

Trick 2: As per other suggestions, I changed the contentInset of the UIScrollView.

In keyboardWillAppear method I added following code:

  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;

and in keyboardWillDisappear method I set the contentInset back to zero values.

Hence, let me know if there needs to be any other way to sort this out or any other possible changes I need to make in scrollview frame. Moreover , if I turn on the bouncesVertically functionality it is able to bounce even when complete subviews are visible onscreen which I don't want. So basically I want it to freeze when keyboard is not there and scrollable till viewable area when it is up. Hence, give me any other suggestions? Thanks in advance.

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

it is useful for me

I can really recommend this library: https://github.com/michaeltyson/TPKeyboardAvoiding

It's very very easy to use, and works for ScrollView, TableView and CollectionView!

From a conceptual point of view when " scrollView Size == scrollView ContentSize ", it does not scroll. To make it scrollable we need to increase the contentSize . In your problem you need to adjust the contentSize of scrollView along with frame. This can be done in your first approach.

As for the second approach, changing the edge insets will create a sort of padding for the content drawable area. This can make the bottom content visible, but it won't affect the contentSize , hence view will not be scrollable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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