robspencer77
robspencer77

Reputation: 149

Why does UIScrollView resize back up when moving to another textfield - iOS iPhone

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;
}

Upvotes: 2

Views: 312

Answers (2)

robspencer77
robspencer77

Reputation: 149

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;
}

Upvotes: 1

Ulle Tad
Ulle Tad

Reputation: 345

In this kind of tasks I'm using BSKeyboardControls

There is good example project and Usage description

Upvotes: 0

Related Questions