1

How can I move UItextview up and down in a UIScrollView in an iphone app when keyboard appear and disappear.

Adam Richardson
  • 2,448
  • 1
  • 25
  • 30
Nila
  • 49
  • 1
  • 3

4 Answers4

5

One of the way is --->in viewDidLoad or ViewWillAppear add these observer

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboardDisappeared) name:UIKeyboardWillHideNotification object:nil];
[center addObserver:self selector:@selector(keyboardAppeared) name:UIKeyboardWillShowNotification object:nil];

than write keyboardDisappeared and keyboardAppeared as follow

-(void) keyboardDisappeared
{
    [UIView beginAnimations:@"animate" context:nil];
    [UIView setAnimationDuration:0.2f];
    [UIView setAnimationBeginsFromCurrentState: NO];
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+100(same as you do in keyboardAppeared), self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

-(void) keyboardAppeared
{
    [UIView beginAnimations:@"animate" context:nil];
    [UIView setAnimationDuration:0.2f];
    [UIView setAnimationBeginsFromCurrentState: NO];
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-(as much you want), self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
}

UPDATE:

SWIFT 3.x/4.x

Add these observer -

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

And the selector methods:

@objc func keyboardWillShow(notification:NSNotification) {
    adjustView(show: true, notification: notification)
}

@objc func keyboardWillHide(notification:NSNotification) {
    adjustView(show: false, notification: notification)
}

func adjustView(show:Bool, notification:NSNotification) {
    var userInfo = notification.userInfo!
    let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
    let changeInHeight = (keyboardFrame.size.height + 40/*or any value as per need*/) * (show ? 1 : -1)
    UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
        // change height Constraint or change height with changeInHeight here to your UItextView/other view
    })

}

don't forget to remove observer!!!

Suryakant Sharma
  • 3,356
  • 1
  • 20
  • 43
3

Just add notification observer for keyboard's action in -viewDidLoad or -viewWillAppear::

NSNotificationCenter * notificationCetner = [NSNotificationCenter defaultCenter];
[notificationCetner addObserver:self
                       selector:@selector(_keyboardWasShown:)
                           name:UIKeyboardDidShowNotification
                         object:nil];
[notificationCetner addObserver:self
                       selector:@selector(_keyboardWillHide:)
                           name:UIKeyboardWillHideNotification
                         object:nil];

Then update your text view's frame in -_keyboardWasShown: & -_keyboardWillHide: methods.

Kjuly
  • 32,573
  • 22
  • 98
  • 112
2

There is a nice library called TPKeyboardAvoiding that you can use (find it here).

Alternatively you could program it yourself and use the contentOffset property of the UIScrollView to move the content down or upwards. You then should be listening to the keyboardDidShow and keyboardWillHide methods of the keyboard.

Hope it helps!

Thermometer
  • 2,368
  • 3
  • 15
  • 39
-1

First set textview's delegate and then try to implement these methods....

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
    {
        [self scrollViewToCenterOfScreen:textView];
        return YES;
    }

    - (void) scrollViewToCenterOfScreen:(UIView *)theView
    {
        CGFloat viewCenterY = theView.center.y;
        CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

        CGFloat availableHeight = applicationFrame.size.height - 300;            // Remove area covered by keyboard

        CGFloat y = viewCenterY - availableHeight / 2.0;

        if (y < 0)
        {
            y = 0;
        }
        [scrollView setContentOffset:CGPointMake(0, y) animated:YES];
    }

// -------- Modified --------


    - (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)tempText
{
    if( [tempText rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound )
    {
        return YES;
    }

    [txtView resignFirstResponder];
    [self.scrollView setContentOffset:CGPointMake(0.0f, 0.0f) animated:TRUE];
    return NO;
}
Dharmbir Singh
  • 16,953
  • 5
  • 48
  • 64