iPhone: позиционирование нескольких полей UITextField при отображении клавиатуры и изменении ориентации - PullRequest
3 голосов
/ 30 марта 2010

У меня есть представление, которое содержит 10 UITextFields, созданных программно. Я хочу следующее поведение:

  1. Когда я нажимаю на определенное UITextField, клавиатура должна скрывать все текстовые поля, которые визуально находятся ниже выделенного текстового поля.
  2. Если у меня выделено текстовое поле и изменена ориентация устройства, текстовое поле и клавиатура должны повернуться в правильную ориентацию, при этом текстовое поле не потеряет фокус выделения.
  3. Мне нужно контролировать, активна ли клавиша возврата на клавиатуре.

Как мне управлять этими текстовыми полями, чтобы получить эти поведения.

1 Ответ

0 голосов
/ 22 марта 2012

Добавьте свое текстовое поле в scrollview и установите тег для всего текстового поля. Затем введите в свое приложение следующий код. Вам необходимо ввести положение текстового поля в соответствии с вашими требованиями.

- (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.
}

}

// Делегат вызывается при изменении ориентации

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {
// Return YES for supported orientations
return YES;

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...