Move upward textField in Scrollview when keyboard appears


Step 1>>First add a view in Interface class

@property(weak, nonatomic) UIView *activeTextView;

Add Notification in ViewWillAppear

– (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    // register for keyboard notifications

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotificationobject:nil];

     [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotificationobject:nil];

    }

Step 2>>

– (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    // unregister for keyboard notifications

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardDidHideNotificationobject:nil];

    }

Step 3>>

– (void)textFieldDidBeginEditing:(UITextField *)textField{

    // save the text view that is being edited

 self.activeTextView = textField;

}

– (void)textFieldDidEndEditing:(UITextField *)textField{

    // release the selected text view as we don’t need it anymore

    self.activeTextView = nil;

}

Step 4>>

– (void)keyboardWasShown:(NSNotification *)aNotification

{

 NSDictionary* info = [aNotification userInfo];

    CGSize kbSize= [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect bkgndRect = activeTextView.superview.frame;

    bkgndRect.size.height += 200;//kbSize.height;

    [activeTextView.superview setFrame:bkgndRect];

    [_scrollViewsetContentOffset:CGPointMake(0.0, activeTextView.frame.origin.y200) animated:YES];

}

 

// Called when the UIKeyboardWillHideNotification is received

– (void)keyboardWillBeHidden:(NSNotification *)aNotification

{

    // scroll back..

   /* UIEdgeInsets contentInsets = UIEdgeInsetsZero;

    _scrollView.contentInset = contentInsets;

    _scrollView.scrollIndicatorInsets = contentInsets;*/

    [self._scrollViewsetFrame:CGRectMake(0, 0, self._scrollView.frame.size.width, self._scrollView.frame.size.height)];

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: