1

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

Adam Richardson
  • 2,518
  • 1
  • 27
  • 31
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,852
  • 1
  • 25
  • 47
  • +1 for using notifications, but how old was the book you copied the UIView animation code from? We've had block based animation for 3 years!!! – Ashley Mills Jul 12 '13 at 13:52
  • doesn't matter @AshleyMills because code never gets old. what matter is, it doesn't affect your program efficiency. – Suryakant Sharma Jul 15 '13 at 05:31
  • 1
    According to Apple's docs - `beginAnimations:context:` **Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.** – Ashley Mills Jul 15 '13 at 13:07
  • Thank you @AshleyMills, I'll keep this in mind. – Suryakant Sharma Jul 16 '13 at 05:37
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
  • 34,476
  • 22
  • 104
  • 118
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,567
  • 3
  • 20
  • 41
-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
  • 17,485
  • 5
  • 50
  • 66