Cuero
Cuero

Reputation: 1209

How to move up a UIScrollView when keyboard appears?

Update:

my question is: how to prevent the scrollview being reset to the position when clicking on another textfield?

Actually I've tried several ways, such as the solution to this question.

Originally, my scrollView (multiple textfield and textview in it, and all of positions are placed in storyboard) is at the position (0, 302). When the keyboard appears, I wanna move it up to (0, 100). Firstly, I tried to add a button and do the following, it works.

CGRect frame = self.informationScrollView.frame;
frame.origin.y = 100;
self.informationScrollView.frame = frame;

Then I tried to do the same code in the selector of UIKeyboardDidShowNotification, but it failed. I found the position are always reset even when the activity textField has changed. Can anyone tell me how to prevent the app reseting the position? Thanks a lot.

Upvotes: 1

Views: 9435

Answers (6)

cotugs
cotugs

Reputation: 234

The best course of action with UIScrollView is to set the contentInsets and the scrollIndicatorInsets based on the appearance of the keyboard. If the content of the scroll view are your UITextField and UITextView instances, then you should be offsetting the scroll view content via contentOffset, not moving the frame of the scroll view.

That being said, if you really need to move the scroll view and its bouncing back, it's probably something to do with the layout constraints.

Upvotes: 1

user4390704
user4390704

Reputation:

Use the best concept of NSNotificationCenter which is explained by @Sarat_Patel

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotification object:nil];

And handle with local methods:

-(void)keyboardWasShown:(NSNotification*)notification
-(void)keyboardWillBeHidden:(NSNotification *)notification

Upvotes: 1

Andrew Ebling
Andrew Ebling

Reputation: 10283

Here's the Official Apple Approved approach: Managing The Keyboard: Text Programming Guide

However, I would recommend if you are using Autolayout, that you use a constraint to manage the space between the UITextView and the bottom of the screen and programatically change it's constant value to match that of the height of the keyboard via the keyboard show notification mentioned in the above article and other solutions here. This is because you can receive multiple keyboard show notifications with different heights, which can break if you try and toggle the view frame.

Upvotes: 0

Mrunal
Mrunal

Reputation: 14118

CGRect frame = self.informationScrollView.frame;

[UIView animateWithDuration:1
                      delay:0
                    options: UIViewAnimationOptionCurveEaseInOut
                 animations:^{

                 self.informationScrollView setFrame:CGRectMake (frame.origin.x,
                                                                 frame.origin.y, 
                                                                 frame.size.width, 
                                               <change height as per requirement>);
                 }
                 completion:nil];

For Multiple TextFields:

How to make a UITextField move up when keyboard is present?

Hope this helps.

Upvotes: 3

iAnurag
iAnurag

Reputation: 9356

If ypu are using a UIScrollView then, you should take a look in TPAvoidingScrollView. Its easy to use and give the expected result. You just have to set TPAvoidingScrollView as your UIScrollView's superclass and everything will be taken care of by TPScrollView. JUST TRY IT. HOPE THIS HELPS.

Upvotes: 3

Sarat Patel
Sarat Patel

Reputation: 856

Its pretty simple Use this into your viewDidLoad method

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotification object:nil];

and write a code for scrollView against keyboard height inside that

-(void)keyboardWasShown:(NSNotification*)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.view.frame.origin.x,self.view.frame.origin.y, kbSize.height+100, 0);
self.scrollViewChildDetail.contentInset = contentInsets;
self.scrollViewChildDetail.scrollIndicatorInsets = contentInsets;
}

-(void)keyboardWillBeHidden:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollViewChildDetail.contentInset = contentInsets;
self.scrollViewChildDetail.scrollIndicatorInsets = contentInsets;
}

it works.

Upvotes: 1

Related Questions