Search This Blog

Oct 5, 2010

UITextField Delegate Methods for Show Hide Keyboard with Animation.

This code is used for Show hide keyboard with animation in iPhone SDK. This example uses UITextField delegate methods.



You can download source code from here Download.

Code for .h file

#import


@interface KeyboardViewController : UIViewController {

    IBOutlet UITextField *txtValue1;
    IBOutlet UITextField *txtValue2;
    IBOutlet UITextField *txtValue3;
}

- (void)setViewMovedUp:(BOOL)movedUp;

@end


Code for .m file.


#import "KeyboardViewController.h"

@implementation KeyboardViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView* imgLogo = [ [ UIImageView alloc] initWithImage: [ UIImage imageNamed:@"apple.png"]];
    self.navigationItem.titleView = imgLogo;
    [imgLogo release];
}

- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = self.view.frame;
    if (movedUp){      
        if(rect.origin.y == 0)
            rect.origin.y = self.view.frame.origin.y - 215;
    }
    else{      
        if(rect.origin.y < 0)
            rect.origin.y = self.view.frame.origin.y + 215;
    }  
    self.view.frame = rect;  
    [UIView commitAnimations];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self setViewMovedUp:YES];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self setViewMovedUp:NO];
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
  
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

2 comments:

  1. From Mehmood:

    thanks. This project helps me so much.

    but one thing you have to tell in this project is to connect text box to its delegate

    ReplyDelete
  2. You can use Keyboard Manager to fix this issue with just **one line of code**. It also **support device orientation**. This is **universal** also.

    Download demo project here
    https://github.com/hackiftekhar/IQClasses/tree/master/KeyboardTextFieldDemo

    Just drag and drop **KeyboardManager** and **SegmenedNextPrevious** classes to your project.
    In your appDelegate write only one line of code:-

    [KeyBoardManager installKeyboardManager];

    This will handle Keyboard and textfield in your app.

    ReplyDelete