I have a UITextField embedded in a UIScrollView, and I want the text field to scroll up to be visible when the keyboard is active. I tried the top answer in How to make a UITextField move up when keyboard is present? but it seems that it does not work with auto-layout, so instead I took the code from the apple docs. This almost works, but its not scrolling the view until the user actually enters some data, rather than when the keyboard actually appears.
Here's what I'm doing - is there something obviously wrong?
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillShow:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:self.activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
[UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"textFieldDidEndEditing");
self.activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"textFieldDidEndEditing");
self.activeField = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self registerForKeyboardNotifications];
}
Try this,
ViewController.h
bool keyboardIsShown;
UITapGestureRecognizer *tapGesture;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
scrollView.contentSize =CGSizeMake(0, self.view.frame.size.height+50);
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
keyboardIsShown = NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
///// KEYBOARD
- (void) moveView:(float) yPos
{
[scrollView setContentOffset:CGPointMake(0, yPos) animated:YES];
}
- (void)keyboardWillHide:(NSNotification *)n
{
keyboardIsShown = NO;
[self.view removeGestureRecognizer:tapGesture];
[self moveView:0.0];
}
- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown)
{
return;
}
keyboardIsShown = YES;
[self.view addGestureRecognizer:tapGesture];
[self moveView:255.0];
}
// method to hide keyboard when user taps on a scrollview
-(void)dismissKeyboard
{
[self.view endEditing:YES];
}
Try the code below:
-(void)keyboardWillShow:(id)sender
{
if ([orientation==#"Landscape"])
[loginScrollView setContentSize:CGSizeMake(480, 620)];
else
[loginScrollView setContentSize:CGSizeMake(320, 670)];
}
and while editing the text use this
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if ([orientation == #"Landscape"]) {
if (textField == usernameTextField)
[loginScrollView setContentOffset:CGPointMake(xValue, 200) animated:YES];
if (textField == passwordTextField)
[loginScrollView setContentOffset:CGPointMake(xValue, 220) animated:YES];
}
else{
if (textField == usernameTextField)
[loginScrollView setContentOffset:CGPointMake(xValue, 168) animated:YES];
if (textField == passwordTextField)
[loginScrollView setContentOffset:CGPointMake(xValue, 172) animated:YES];
}
}
Related
I have very strange issue which happens when keyboard appears they should shift textfield. It's work nice until I open and close Control Center (Video link)
In video I open and close Control Center fast and immediately press to textField, but if open and close Control Center and await around one second, it will work correctly.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardShow:) name: UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillResignActiveSelector:) name:#"applicationWillResignActive" object:nil];
}
- (void) viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void) applicationWillResignActiveSelector:(id)object {
[self dismissKeyboard];
}
- (void)dismissKeyboard {
if (self.textInput.isFirstResponder) {
[self.textInput resignFirstResponder];
}
}
- (void)keyboardShow:(NSNotification *)notification {
CGRect keyboard = [[info valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval duration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
self.view.center = CGPointMake(self.centerView.x, self.centerView.y - heightKeyboard);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, heightKeyboard - self.view.safeAreaInsets.top, 0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.bounds.size.width - 10);
[self.view layoutIfNeeded];
}
- (void)keyboardHide:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
NSTimeInterval duration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
self.tableView.delegate = nil;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.view.center = self.centerView;
self.chatBackgroundTopConstraint.constant = 0;
[self.view layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, self.tableView.contentInset.left, self.tableView.contentInset.right, self.tableView.bounds.size.width - 10);
} completion:^(BOOL finished) {
self.tableView.delegate = self;
}];
[self.tableView reloadData];
}
I am new to iOS, just started to design an login form. when user click the textfield, keypad appears which hide the textfield so I unable to see the textfield.
So how I animate the textfield to top at the time of keypad appear.
You can try this->
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == ChatField)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField == ChatField) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}return YES;
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float newVerticalPosition = -keyboardSize.height + 10;
[self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
CGFloat kNavBarHeight = self.navigationController.navigationBar.frame.size.height;
float newVerticalPosition = kNavBarHeight -45;
[self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];
}
- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration {
CGRect frame = self.view.frame;
frame.origin.y = position;
[UIView animateWithDuration:duration animations:^{
self.view.frame = frame;
}];
}
I need a way to be able to check if the keyboard when it shows up hides any element in the view. If so, i need the view to move up in a way that the element is shown but without the navigation bar moving.
Thanks in advance
#import "RequestViewController.h"
#define kOFFSET_FOR_KEYBOARD 80.0
#interface RequestViewController ()
#end
#implementation RequestViewController{
CGFloat keyboardHeight;
}
#synthesize descirptionTextView;
#synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view
descirptionTextView.text = #"Comment";
descirptionTextView.textColor = [UIColor lightGrayColor];
descirptionTextView.delegate = self;
descirptionTextView.layer.cornerRadius = 8;
// border
[descirptionTextView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[descirptionTextView.layer setBorderWidth:0.5f];
// drop shadow
[descirptionTextView.layer setShadowColor:[UIColor blackColor].CGColor];
[descirptionTextView.layer setShadowOpacity:0.8];
[descirptionTextView.layer setShadowRadius:3.0];
[descirptionTextView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
descirptionTextView.text = #"";
descirptionTextView.textColor = [UIColor blackColor];
return YES;
}
-(void) textViewDidChange:(UITextView *)textView
{
if(descirptionTextView.text.length == 0){
descirptionTextView.textColor = [UIColor lightGrayColor];
descirptionTextView.text = #"Comment";
[descirptionTextView resignFirstResponder];
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - Scrolling out of keyboard way
-(void)keyboardWillShow:(NSNotification *)nsNotification{
//first, get height of keyboard
NSDictionary *userInfo = [nsNotification userInfo];
CGRect kbRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
keyboardHeight = kbRect.size.height;
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, self.view.frame.size.height - keyboardHeight - scrollView.frame.origin.y);
return;
}
-(void)keyboardWillHide{
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + keyboardHeight - 40 - 40 - 14 + scrollView.frame.origin.y);
return;
}
You can redraw your views in the keyboard delegate methods:
keyboardWillShow
keyboardWillHide
Declare a CGFloat property named keyboardHeight.
In your viewDidLoad method:
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
Keyboard methods:
-(void)keyboardWillShow:(NSNotification *)nsNotification{
//first, get height of keyboard
NSDictionary *userInfo = [nsNotification userInfo];
CGRect kbRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
keyboardHeight = kbRect.size.height;
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, self.view.frame.size.height - keyboardHeight - scrollView.frame.origin.y);
return;
}
-(void)keyboardWillHide{
scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + keyboardHeight - 40 - 40 - 14 + scrollView.frame.origin.y);
return;
}
You should also be able to substitute scrollView with self.view
When the keyboard appears, you are reducing the visible area by a considerable amount, so unless you have a layout that can be re-run with the smaller area, what you really desire is the ability to scroll.
In general, you'll want to choose where you scroll to based on which field is the firstResponder. This will guarantee that a user is never editing a field that they cannot see.
Revise your view hierarchy for this controller to be contained within a UIScrollView. Also, track which field is the firstResponder in an instance variable. Then, respond to keyboard notifications like this:
- (void)keyboardWillShow:(NSNotification*)notification
{
NSValue* keyboardFrameValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect kbRect = [self.view convertRect:keyboardFrameValue.CGRectValue fromView:nil];
CGRect overlap = CGRectIntersection(self.view.bounds, kbRect);
self.scroller.contentInset = UIEdgeInsetsMake(self.scroller.contentInset.top, 0, overlap.size.height, 0);
if (self.firstResponderView)
{
CGRect fieldRect = [self.scroller convertRect:self.firstResponderView.frame fromView:self.firstResponderView.superview];
[self.scroller scrollRectToVisible:fieldRect animated:YES];
}
}
Well, one can use a library like this one: https://github.com/michaeltyson/TPKeyboardAvoiding, or he can do it programmatically. This is how I proceeded:
//adding the notification about when keyboard appears and disappears when the view loads and remove them when the view will disappear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
Then you add these selector methods
#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
if(keyboardSize.height> (self.view.frame.size.height - YourTextField.frame.size.height -YourTextField.frame.origin.y)){
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = self.view.frame.size.height - YourTextField.frame.size.height -YourTextField.frame.origin.y-keyboardSize.height - 10;
self.view.frame = f;
}];
}
}
-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}
I am very junior mobile programmer .I need to move up text views when keyboard appears.I follows this move-uiview-up-when-the-keyboard-appears-in-ios and it works well but I have a background image and I do not want to move up background image .so all textboxes are embed in UIView named as customView.I tried to move up customView instead of self.view .When I start enter in first textview, the customView moves up.But when I move to second textview,customview moves down to original position and textView become under the keyboard.customView need to stay move up the when i start enter in second textview .I really appreciate any help!.
#property (strong, nonatomic) IBOutlet UIView *customView;
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES; }
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES; }
- (void)keyboardDidShow:(NSNotification *)notification
{
//Assign new frame to your view
[self.customView setFrame:CGRectMake(0,50,320,460)];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.customView setFrame:CGRectMake(0,193,320,460)];
}
Add the observer in viewDidLoad for best approach.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification {
[UIView animateWithDuration:0.25 animations:^
{
CGRect newFrame = [customView frame];
newFrame.origin.y -= 50; // tweak here to adjust the moving position
[customView setFrame:newFrame];
}completion:^(BOOL finished)
{
}];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
[UIView animateWithDuration:0.25 animations:^
{
CGRect newFrame = [customView frame];
newFrame.origin.y += 50; // tweak here to adjust the moving position
[customView setFrame:newFrame];
}completion:^(BOOL finished)
{
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
// Add a scrollview on main view and add UITextField on that scrollview
-(void) viewDidLoad
{
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
myScrollView.contentSize = CGSizeMake(320, 500);
myScrollView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
[self.view addSubview:myScrollView];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,30,100,33)];
[myScrollView addSubview:myTextField];
myTextField.delegate = self;
}
// Set the scrollview content offset to make the myTextField move up
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[myScrollView setContentOffset:CGPointMake(0,textField.center.y-80) animated:YES];
// here '80' can be any number which decide the height that textfiled should move
}
//To move the textfield to its original position
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[[myScrollView setContentOffset:CGPointMake(0,0) animated:YES];
[textField resignFirstResponder];
return YES;
}
Make your class implement the UITextFieldDelegate.
Put the following code in viewDidLoad.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
And define the following functions in your .m file.
- (void)keyboardWasShown:(NSNotification *)aNotification
{// scroll to the text view
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible.
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
self.scrollView.scrollEnabled = YES;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)aNotification
{
// scroll back..
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
self.scrollView.scrollEnabled = NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
This probably isn't an easy one to answer....
I have 8 text fields on screen which sit in a scrollView. When I select the first field, the keyboard appears and the scrollView shrinks. I can then scroll up and down.
When I select another text field, I can't scroll anymore.
I've checked the scrollView height using textFieldDidBeginEditing and it definitely grows back up.
I've also registered for all 4 of the keyboard notifications but none of them are triggered during the move to the new text field.
Any pointers would be much appreciated :)
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
keyboardIsShown = NO;
//Additional Code
}
- (void)viewDidUnload {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height += (keyboardSize.height - self.toolBar.frame.size.height);
[self.scrollView setFrame:viewFrame];
keyboardIsShown = NO;
}
- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = [n userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height -= (keyboardSize.height - self.toolBar.frame.size.height);
scrollViewHeight = viewFrame.size.height;
[self.scrollView setFrame:viewFrame];
keyboardIsShown = YES;
}
For moving between the text fields:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[[self saveButton] setEnabled:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.label1) {
[self.label2 becomeFirstResponder];
} else if (textField == self.label2) {
[self.label3 becomeFirstResponder];
} else if (textField == self.label3) {
[self.label4 becomeFirstResponder];
} else if (textField == self.label4) {
[self.label5 becomeFirstResponder];
} else if (textField == self.label5) {
[self.label6 becomeFirstResponder];
} else if (textField == self.label6) {
[self.label7 becomeFirstResponder];
} else if (textField == self.label7) {
[self.label8 becomeFirstResponder];
} else if (textField == self.label8) {
[textField resignFirstResponder];
}
return YES;
}
After lots of digging I found the following code, which I've now tweaked and tested:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
keyboardIsShown = NO;
//Additional code
}
- (void)viewDidUnload {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (kbSize.height - self.toolBar.frame.size.height), 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= (kbSize.height + (self.toolBar.frame.size.height*2));
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
In this kind of tasks I'm using BSKeyboardControls
There is good example project and Usage description