This is an easy solution of the problem like keyboard hides UITextField in IOS.
Just refer your UITextField to its delegate and object in .xib file and write down the following code.
ViewController.h
================
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>{
IBOutlet UITextField *nameTf,*addressTf;
}
@end
ViewController.m
================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int movementDistance=0;
const float movementDuration = 0.3f;
if (textField == nameTf) {
movementDistance = 200;//you can change this value
}else if (textField == addressTf) {
movementDistance = 210;//you can change this value
}
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];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
@end
Just refer your UITextField to its delegate and object in .xib file and write down the following code.
ViewController.h
================
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>{
IBOutlet UITextField *nameTf,*addressTf;
}
@end
ViewController.m
================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int movementDistance=0;
const float movementDuration = 0.3f;
if (textField == nameTf) {
movementDistance = 200;//you can change this value
}else if (textField == addressTf) {
movementDistance = 210;//you can change this value
}
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];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
@end
No comments:
Post a Comment