iPhone: Positioning Multiple UITextFields When Keyboard is Shown and Orientation Changes
I have a view that contains 10 UITextFields created programatically. I want the following behavior:
- When I click on a particular UITextField, the keyboard should hide all the text fields wh开发者_StackOverflow社区ich are visually below the selected text field.
- If I have a text field selected and change the device orientation, the text field and the keyboard should both rotate to the proper orientation without the text field losing the selection focus.
- I need to control whether the return key in the keyboard is active.
How do I manage these text fields to get these behaviors.
Add your textfield in scrollview and set tag for all the textfield.Than put following code in you application.You must have to enter you textfield position as per your requirements.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Begin animations to move TextFields into view.
if (textField.tag == 1) {
[UIView beginAnimations: @"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,30,320,357);
[UIView commitAnimations];
textfield2.hidden=YES;
textfield3.hidden=YES;
textfield4.hidden=YES;
}
else if(textField.tag == 2)
{
[UIView beginAnimations: @"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,30,320,357);
[UIView commitAnimations];
textfield1.hidden=YES;
textfield3.hidden=YES;
textfield4.hidden=YES;
}
else if(textField.tag == 3)
{
[UIView beginAnimations: @"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,25,320,357);
[UIView commitAnimations];
textfield1.hidden=YES;
textfield2.hidden=YES;
textfield4.hidden=YES;
}
else if(textField.tag == 4)
{
[UIView beginAnimations: @"moveField" context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self.scrlview.frame = CGRectMake(0,20,320,357);
[UIView commitAnimations];
textfield1.hidden=YES;
textfield2.hidden=YES;
textfield3.hidden=YES;
}
return YES;
}
//Set the objects on the view when device orientation will change.
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationLandscapeRight) {
// set the views oreintation here for landscapemode.
}
if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ||
interfaceOrientation == UIInterfaceOrientationPortrait) {
//set the views oreintation here for Portraitmode.
}
}
//Delegate called when orientation will change
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
精彩评论