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
My .m
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.
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
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.