1

I am a fairly new iPhone developer, and I am working on an iPhone app that has a view where the user needs to enter input into multiple UITextViews. There are a total of 6 UITextViews and when the view appears all the text views are visible without the need to scroll. But when the user clicks on the first text view to enter the text, the last 2 text views become hidden by the keyboard and I can't figure out how to add scrolling capability so the user will be able to scroll when the keyboard is visible. I am using a UIScrollView but currently have no code to make it work since I have tried multiple different things I have found online and none have worked. This may be an easy solution, but I am just out of ideas and have been stuck for a while. Any advice is greatly appreciated. Thanks

More Info: I am using the latest version of Xcode, developing for iPhone versions 6.1 and above. I used the Interface Builder to set up the ScrollView and the AutoLayout box is checked.

Matt Vella
  • 35
  • 6

5 Answers5

2
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{
    /* this returns the keyboard when user click on return button which is reside on keyboard */

    if([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        [yourscrollview setContentOffset:CGPointMake(0,0)animated:YES];
    }
    return YES;
}

-(void)textViewDidEndEditing:(UITextView *)textView

{

 /* it used for hide keyboard and set all control on their original position */

}

-(void)textViewDidBeginEditing:(UITextView *)textView

{

 /* depending upon condition it will scroll the textview so textview can't reside behind the keyboard */

   [yourscrollview setContentOffset:CGPointMake(0,textView.center.y-80)animated:YES];

}

above 80 i was defined because my requirement is to take textview that much up when keyboard appears you can put a value which is suitable to your requirements

Maul
  • 1,141
  • 6
  • 13
2

in your view did load write following lines

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide) name:UIKeyboardWillHideNotification object:nil];

Make following methods.

-(void)keyboardDidHide
{
    scrollview.frame = YOUR_ORIGINAL_FRAME;//You should set frame when keyboard is not there
    scrollview.contentSize=scrollview.frame.size;
}
-(void)keyboardDidShow
{
    CGRect r = scrollview.frame;
    scrollview.contentSize=scrollview.frame.size;
    r.size.height - = 216;//216 is keyboard height for iPhone.
    scrollview.frame = r;
}
Nirav Gadhiya
  • 6,232
  • 2
  • 33
  • 75
  • Welcome Feel free to ask other questions – Nirav Gadhiya Nov 07 '13 at 09:04
  • Like I mentioned before, I am new to iOS development so I hope this isn't a stupid question, but how do I get the same scrolling behavior when the iPhone changes to landscape? It works great in portrait, but it doesn't work in landscape. – Matt Vella Nov 08 '13 at 16:47
  • I actually just asked this question, here is the link: http://stackoverflow.com/questions/19876716/how-do-i-get-a-textview-to-expand-when-screen-is-rotated-from-portrait-to-landsc – Matt Vella Nov 23 '13 at 05:53
  • I should start checking this more often because that answer is perfect, thanks again! – Matt Vella Jan 03 '14 at 05:23
  • Since you have been able to answer all of my questions so far, if you get a chance can you take a look at this one: http://stackoverflow.com/questions/21490012/when-dismissing-keyboard-how-do-i-keep-my-existing-text-fields-in-the-same-locat – Matt Vella Feb 01 '14 at 06:57
0

Follow 2 simple steps as following

  1. From storyboard/xib, adjust the frame for your scrollview and keep height as screen size.

  2. Within your viewcontroller apply the contentsize for your view like,

            <scrollview>.contentSize=CGSizeMake(320, 700);
    

You will be able to see your entire scrollview on scrolling.

Niru Mukund Shah
  • 4,305
  • 2
  • 17
  • 34
0

Use following link to move textview or textfield up and down automatically when keyboard appears

https://github.com/michaeltyson/TPKeyboardAvoiding

this link contains demo project. you can use this as you requirement

i hope this will help you.

Bhavesh Lakum
  • 130
  • 1
  • 12
0

You could do it this way:

// In View First add keyboard appearance and disappearance Notifications

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];

// Now inside this selector method
-(void)keyboardWillShow:(NSNotification *)notify
{
    if (!notify) {
        return;
    }

    NSDictionary *userInfo = [notify userInfo];

    NSValue *keyboardEndFrame = ([userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]);

    CGRect endFrame = keyboardEndFrame.CGRectValue;

    // Change the frame of the view to its parent
    CGRect loginViewFrame = [loginView.superview convertRect:loginView.frame fromView:loginView];

    // Check the keyboard covers the view 
    if (CGRectGetMinY(endFrame) < CGRectGetMaxY(loginViewFrame))
    {
        // If YES calculate Distance. Save this difference to animate back the view 
        difference = CGRectGetMaxY(loginViewFrame)- CGRectGetMinY(endFrame);

        // animate that View
        [self animateViewUp:YES withMovementDistance:difference];
    }
}

// inside Will Hide
-(void)keyboardWillHide
{
    // Animate back the view with the calculated distance
    [self animateViewUp:NO withMovementDistance:difference];
}

- (void)animateViewUp:(BOOL)up withMovementDistance:(int)movementDistance
{
    const float movementDuration = 0.3f;

    int movement = (up ? -movementDistance : movementDistance);

    [UIView animateWithDuration:movementDuration animations:^{
    loginView.frame = CGRectOffset(loginView.frame, 0, movement);
    }]; 
}
Marcin Nabiałek
  • 98,126
  • 37
  • 219
  • 261