I need to push the entire up (it has scrollview) when keyboard shown
but can't seem to get it working with this code.
All the code being hit but it just doesn't push up the view.
I am following the example here.
http://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeField = 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);
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.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, self.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;
}
I imagine that the keyboard appears when the user clicks on a textfield right?
you have to use the method of the delegate for the UITextField.
in your viewController.h add UITextFieldDelegate and in your .m insert:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField *)textField up: (BOOL) up
{
const int movementDistance = 140; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
add also these methods:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[yourTextField resignFirstResponder];
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
[yourTextField resignFirstResponder];
Return YES;
}
you can use scrollView in your view. then put all other subviews in the scrollView and then set the content offset of scrollView in "textfieldShouldBeginEditing" method.
[scrollView setContentOffset:CGPointMake(0,160)]; // this is just an example
hope this will help you.
I had the same problem! Instead I decided to use a tableview. Insert your UITextFields in the TableViewCells and it will scroll up automatically when keyboard comes up!
Related
In my application, I have a signing up form, with some textFields (Email, password etc ...), when I begin typing somewhere in these textFields (The keyboard is shown), my View moves Up, and I'm not able to see what's being typed in my text field.
This problem occurs only with iOS8.
If some one knows how to block this os feature, i'll be thankful.
Regards.
Here is my code for managing issues related to Keyboard appearance and disappearance.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self registerForKeyboardNotifications];
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - Keyboard Delegate
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//to overcome the bug with keyboard height for os7 and earlier
if (![UIDeviceHardware isOS8Device]) {
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect fromView:nil];
kbSize = kbRect.size;
}
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollView.contentInset = contentInsets;
_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;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[_scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
}
You can pick activeField from UITextfield Delegate.
This will be useful to you..
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
{
CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y - 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
[UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
[textField becomeFirstResponder];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
{
CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y + 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
[UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
[textField resignFirstResponder];
}
}
I have UIScrollView and UITextField like this. I know how to get keyboard height shown in my self.view. However, UITextField position will not be fixed at particular position since user may scroll.
How can I move up the uitextfield only if it is covered by keyboard? Do I need to use CGPoint and convertPoint? May I know how to do?
You should use UITextFieldDelegate.
Here is an example how you can implement it :
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if(!moved) {
[self animateViewToPosition:self.view directionUP:YES];
moved = YES; }
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if(moved) {
[self animateViewToPosition:self.view directionUP:NO];
moved = NO;
}
[textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if(moved) {
[self animateViewToPosition:self.view directionUP:NO];
}
moved = NO;
return YES;
}
-(void)animateViewToPosition:(UIView *)viewToMove directionUP:(BOOL)up {
const int movementDistance = -260; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
viewToMove.frame = CGRectOffset(viewToMove.frame, 0, movement);
[UIView commitAnimations];
}
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (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);
scrollView.contentInset = contentInsets;
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;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
it may help if you some one wants to try but it may not the best solution
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect actualframe = [textField convertRect:[textField bounds] toView:self.view];
if ((actualframe.origin.y) >= (self.view.frame.size.height-keyboardheignt))
{
CGRect scrollfrm = [textField convertRect:[textField bounds] toView:tableview];
[tableview setContentOffset:CGPointMake(0, scrollfrm.origin.y) animated:YES];//60
}
}
you have to use the content inset if the textfield is last row of the table
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;
}
I am using UITableView, where each cell contains a UITextField, and using Apple official example, I wish to perform goal, moving the active field above the keyboard if it's already hidden. However, the form doesn't move although the UITableView is scroll eabled. Here is my relevant code, please help me pointing on what seems to be missing/wrong:
#interface ViewController (){
UITextField *activeField;
}
- (void)viewDidLoad
{
//....
objTableView.scrollEnabled=YES;
//....
}
#pragma mark - Keyboard notifications
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (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);
objTableView.contentInset = contentInsets;
objTableView.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, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[objTableView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
objTableView.contentInset = contentInsets;
objTableView.scrollIndicatorInsets = contentInsets;
}
#pragma mark - UITextFieldDelegate protocol method
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
Try this code:
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGRect rc = [activeField bounds];
rc = [activeField convertRect:rc toView:objTableView];
[objTableView scrollRectToVisible:rc animated:YES];
}
I trying to push my view up when the keyboard is showing (it overlays data I want the user to see while he's typing. I'm using this code:
KBKeyboardHandler.h:
#protocol KBKeyboardHandlerDelegate;
#interface KBKeyboardHandler : NSObject
- (id)init;
// Put 'weak' instead of 'assign' if you use ARC
#property(nonatomic, assign) id<KBKeyboardHandlerDelegate> delegate;
#property(nonatomic) CGRect frame;
#end
KBKeyboardHandler.m:
#import "KBKeyboardHandler.h"
#import "KBKeyboardHandlerDelegate.h"
#implementation KBKeyboardHandler
- (id)init
{
self = [super init];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
#synthesize delegate;
#synthesize frame;
- (void)keyboardWillShow:(NSNotification *)notification
{
CGRect oldFrame = self.frame;
[self retrieveFrameFromNotification:notification];
if (oldFrame.size.height != self.frame.size.height)
{
CGSize delta = CGSizeMake(self.frame.size.width - oldFrame.size.width,
self.frame.size.height - oldFrame.size.height);
if (self.delegate)
[self notifySizeChanged:delta notification:notification];
}
}
- (void)keyboardWillHide:(NSNotification *)notification
{
if (self.frame.size.height > 0.0)
{
[self retrieveFrameFromNotification:notification];
CGSize delta = CGSizeMake(-self.frame.size.width, -self.frame.size.height);
if (self.delegate)
[self notifySizeChanged:delta notification:notification];
}
self.frame = CGRectZero;
}
- (void)retrieveFrameFromNotification:(NSNotification *)notification
{
CGRect keyboardRect;
[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardRect];
self.frame = [[UIApplication sharedApplication].keyWindow.rootViewController.view convertRect:keyboardRect fromView:nil];
}
- (void)notifySizeChanged:(CGSize)delta notification:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
UIViewAnimationCurve curve;
[[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&curve];
NSTimeInterval duration;
[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&duration];
void (^action)(void) = ^{
[self.delegate keyboardSizeChanged:delta];
};
[UIView animateWithDuration:duration
delay:0.0
options:curve
animations:action
completion:nil];
}
#end
KBKeyboardHandlerDelegate.h:
#protocol KBKeyboardHandlerDelegate
- (void)keyboardSizeChanged:(CGSize)delta;
#end
Sample MyViewController.h:
#interface MyViewController : UIViewController<KBKeyboardHandlerDelegate>
...
#end
Sample MyViewController.m:
#implementation MyViewController
{
KBKeyboardHandler *keyboard;
}
- (void)dealloc
{
keyboard.delegate = nil;
[keyboard release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
keyboard = [[KBKeyboardHandler alloc] init];
keyboard.delegate = self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
keyboard.delegate = nil;
[keyboard release];
keyboard = nil;
}
- (void)keyboardSizeChanged:(CGSize)delta
{
// Resize / reposition your views here. All actions performed here
// will appear animated.
// delta is the difference between the previous size of the keyboard
// and the new one.
// For instance when the keyboard is shown,
// delta may has width=768, height=264,
// when the keyboard is hidden: width=-768, height=-264.
// Use keyboard.frame.size to get the real keyboard size.
// Sample:
CGRect frame = self.view.frame;
frame.size.height -= delta.height;
self.view.frame = frame;
}
That I've found here. I'm trying to figure out why my view isn't pushing up. The keyboard is showing and keyboardSizeChanged is fired up but the view doesn't move. I opened a new project and copied the KBKeyboardHandler and delegate files to it, implemented the code and in the new project it works fine so I know for a fact that it's something in my original project that is messed up. Any idea what it can be?
If you are not using a UITableView use UIScrollView as the parent view of your elements and add the code below
Adjust as needed,
You will need a NSArray of NSObjects, I called mine scrollToObjects and in viewDidLoad add the elements to the array that would need scrolling so that when keyboardWasShown is called you can check to see if the currently selected element should be scrolled to.
I used self.activeField to store my currently selected element which is set when a element triggers an event(the event needs to be one of the first called and that is always called)
Then I check to see if the scrollToObjects contains activeField and if it does scroll the UIScrollView up and then back down when the activeField resignsFirstResponder
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] 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;
// Wouldn't go true so used an array that contains text fields that need to be scrolled to
// if (!CGRectContainsPoint(aRect, self.activeField.frame.origin) ) {
// CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y- kbSize.height);
// [self.scrollView setContentOffset:scrollPoint animated:YES];
// }
if ([self.scrollToObjects containsObject:self.activeField]) {
CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y+ (self.activeField.frame.size.height*2)-kbSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
// self.scrollView.contentInset = UIEdgeInsetsZero;
[self.scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
self.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
A simpler solution would be to handle the UITextFieldDelegate methods below with following code or something similar to activation and deactivation of the object
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint scrollPoint = CGPointMake(0.0, textField.frame.origin.y);
[self.theScrollView setContentOffset:scrollPoint animated:YES];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self.theScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
self.theScrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
I'd check your calculation for delta.
CGSize delta = CGSizeMake(self.frame.size.width - oldFrame.size.width,
self.frame.size.height - oldFrame.size.height);
My guess is oldFrame and self.frame might have the same dimensions.
I'm trying to think if there's anything else that could have been missed, like properly linking views to these objects or something in the interface editor?
The problem is in keyboardSizeChanged: - what you want to adjust is frame.origin.y, not frame.size.height. At least that is what I wanted it to do.
You can also try another library, it's called TPKeyboardAvoiding. It is working great, and is very easy to setup:
Add a UIScrollView into your view controller's xib
Set the scroll view's class to TPKeyboardAvoidingScrollView (still
in the xib, via the identity inspector)
Place all your controls within that scrollview
It also automatically hooks up "Next" buttons on the keyboard to switch through the text fields.