3

I have a view that contains 10 UITextFields created programatically. I want the following behavior:

  1. When I click on a particular UITextField, the keyboard should hide all the text fields which are visually below the selected text field.
  2. 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.
  3. I need to control whether the return key in the keyboard is active.

How do I manage these text fields to get these behaviors.

TechZen
  • 64,370
  • 15
  • 118
  • 145
user217572
  • 185
  • 1
  • 5
  • 10
  • Could you rephrase that and make it understandable? You want the keyboard to come up? – Rengers Mar 30 '10 at 12:28
  • I have five fundamental questions I have 1 View.that view has 10 UITextfields .these are not put on xib file but instead of that I had written code for that. 1>when i click on UITextfield,my keyboard should hide all UITextfields which are under that UITextfield which i clicked 2>if i click on UITextfield and with out pressing return if i autorotate that view whatever way it is in portrait view it should be same in landscape view and viceversa. 3> if i click on UITextfield and with pressing return if i autorotate that view whatever way it is in portrait view it should be same in landscapevie – user217572 Mar 30 '10 at 13:24
  • it means for example i'm on 4 th UItextfield ,when i click on that UITextfield,keyboard hide all the textfields which are below it and only that UITextfield will be above keyboard . Now,if my view is in portrait mode and i switch to landscape mode – user217572 Mar 30 '10 at 13:50
  • it should be seen same as in portrait mode – user217572 Mar 30 '10 at 13:51
  • Did you try changing the frame of the view when the device rotates? As per making the respective textfield the first responder you will have to keep track of them to see which was the first responder before the rotation :) – ThE uSeFuL Sep 21 '11 at 12:04

1 Answers1

0

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;

}

Nitin
  • 7,455
  • 2
  • 32
  • 51