1

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.

Community
  • 1
  • 1
Cuero
  • 1,069
  • 3
  • 18
  • 39

6 Answers6

3
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.

Community
  • 1
  • 1
Mrunal
  • 13,474
  • 6
  • 48
  • 91
  • No, what I wanna change is frame.origin.y – Cuero Dec 17 '14 at 09:04
  • Update frame values as per your requirements, that was just an example code. In your case, give Y value as (frame.origin.y - ) – Mrunal Dec 17 '14 at 09:09
  • Yes, it works at start. But when I click on another textField, the scrollView goes back to the original position (as set in storyboard), how can I prevent it? – Cuero Dec 17 '14 at 09:20
  • In this scenario, i would suggest you to do another thing. Check my update in answer. – Mrunal Dec 17 '14 at 09:26
  • I also pasted the link at my question. – Cuero Dec 17 '14 at 13:49
3

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.

iAnurag
  • 8,752
  • 3
  • 26
  • 45
  • Moar upvotes for this, please. Trivially easy drop-in, very permissive license -- no reason not to do it this way. – user435779 Jul 07 '16 at 14:10
1

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.

ChintaN -Maddy- Ramani
  • 5,006
  • 1
  • 24
  • 46
Sarat Patel
  • 848
  • 13
  • 32
  • Thanks, but it seems only moving the content of scrollview up, but not the scrollview. – Cuero Dec 17 '14 at 09:14
  • yep, it same which you want. its bad programming to move scrollview while keyboard is show. we are set here a content of scrollview not full scrollView. – Sarat Patel Dec 17 '14 at 09:22
  • But I also wanna move up the scrollView, for at first the content only shows at the bottom of the phone and when the keyboard appears, the keyboard will hide all of the scrollview. Thus I need move up the whole scrollView – Cuero Dec 17 '14 at 09:26
  • then simply you've set the scrollview frame in above methods. – Sarat Patel Dec 17 '14 at 09:28
  • Then it's what my problem about. The frame of the scrollView is always reset when clicking on another textField. – Cuero Dec 17 '14 at 09:31
1

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
1

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.

cotugs
  • 234
  • 1
  • 5
  • Thanks. Maybe I should read more examples since I'm new to iOS developing and not so familiar with contentInsets, scrollIndicatorInsets and contentOffset. What I can do is ti copy the code from examples from 'Managing The Keyboard: Text Programming Guide' but not know why the code is that. – Cuero Dec 25 '14 at 01:29
0

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.

Andrew Ebling
  • 9,531
  • 7
  • 51
  • 72