Scroll View not scrolling with keyboard properly

Discussion in 'iOS Programming' started by newtoiphonesdk, Dec 6, 2011.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    The scroll view is not scrolling up to keep text fields from being hidden by the keyboard. Also, when I select a text field that is above where the keyboard will go, it is still scrolling it up even farther away. Here is the .h
    Code:
    @interface ViewController : UIViewController <UINavigationControllerDelegate,  UIScrollViewDelegate> {
    	
    	UITextField *textField;
    	IBOutlet UIScrollView *scrollView;
    	UITextField *activeField;
    
    	BOOL keyboardShown;
    	
    	CGPoint  offset;
    	
    }
    
    
    @property (nonatomic, retain) IBOutlet UITextField *textField;
    @property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
    
    
    @end
    
    My .m
    Code:
    #import "ViewController.h"
    #import <QuartzCore/QuartzCore.h>
    
    @implementation ViewController
    
    @synthesize textField;
    
    @synthesize scrollView;
    
    - (void)viewDidLoad {
    	
    	sleep(3);
        [super viewDidLoad];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillBeHidden:)
                                                     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, 0.0);
        
        UIScrollView *scrollView = (UIScrollView *)self.view;
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
        
        CGRect aRect = self.view.frame;
        aRect.size.height -= kbSize.height;
        
        if (!CGRectContainsPoint(aRect, textField.frame.origin) ) {
            
            CGPoint scrollPoint = CGPointMake(0.0, (textField.frame.origin.y+60)-kbSize.height);
            [scrollView setContentOffset:scrollPoint animated:YES];
            
        }
        
    }
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        
        UIScrollView *scrollView = (UIScrollView *)self.view;
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    }
    - (void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
    	for (UIView* view in self.view.subviews) {
    		if ([view isKindOfClass:[UITextField class]])
    			[view resignFirstResponder];
    	}
    }
    - (BOOL)textFieldShouldReturn: (UITextField *)textField {
    	[textField resignFirstResponder];
    	return YES;
    }
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        activeField = textField;
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField {
        activeField = nil;
    }
    Any thoughts on why this is not moving up text fields that are hidden by keyboard, and scrolling up text fields that aren't hidden? In the .xib I have about 10 TextFields, I set each as delegate.
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Animate the scroll view's frame changing so that the keyboard doesn't cover it up.
     
  3. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Ummm...isn't my code doing that? If you read the post you will see that it is moving the scroll view...just not enough for the text fields that are covered up, and too much for ones that aren't even covered to begin with.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    The suggestion was to alter the frame of the scrollview. I don't see you setting the frame in keyboardWasShown: so no, you are not.
     
  5. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #5
    Oh, and make the notification you're looking for UIKeyboardWillShowNotification or whatever the one that gets sent just before the keyboard shows... that way you can animate your scroll view frame changing size at the exact same time as the keyboard comes up. The keyboard coming up animation takes about 0.3 seconds.

    Looking at the Stanford iOS Tutorial video available free on iTunes U (it's a category in the iTunes Store) on UIScrollViews would probably help you, if you're confused as to how what I'm suggesting you do is different from what you're doing.
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    The code looks OK to me. Using the contentInsets instead of the frame is fine, maybe better. You're not animating the changes but that doesn't matter.

    You need to debug this. Is your scrollView outlet set correctly? Step through the code and look at the values for keyboard height and your contentOffset calculation. Then you should be able to answer your own question.
     
  7. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    Isn't something like this easier?

    Code:
    #pragma mark - UITextField Animation && delegates
    - (void) animateTextField:(UITextField*)textField up:(BOOL)up {
        const int movementDistance = 140;
        const float movementDuration = 0.3f;
        
        int movement = (up ? -movementDistance : movementDistance);
        
        [UIView beginAnimations:@"anim" context:nil];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:movementDuration];
        yourScrolLView.frame = CGRectOffset(yourScrollView.frame, 0, movement);
        [UIView commitAnimations];
    }
    
    - (void) textFieldDidBeginEditing:(UITextField *)textField {
        [self animateTextField:textField up:YES];
    }
    
    - (void) textFieldDidEndEditing:(UITextField *)textField {
        [self animateTextField:textField up:NO];
    }
    Or set the offset of the scrollView, something like that.
    (these are old animations, not new animation blocks, but will still work.)
     
  8. newtoiphonesdk thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #8
    May be easier, but I have multiple text fields at different heights that are covered by keyboard so can't use something with a preset height to raise it
     
  9. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #9
    You can actually change the code a bit, to take an extra paremeter like INT, and instead of calculating height yourself, just take the int. Then you decide on every textfield what you want to move up, but okay.
     

Share This Page