Как немного перетащить мой взгляд при появлении клавиатуры? - PullRequest
0 голосов
/ 12 апреля 2011

В моем приложении у меня есть несколько текстовых полей, которые заполняются через клавиатуру. Их 6, а последние два находятся в самом низу экрана, и он скрывается при появлении клавиатуры. Любые предложения ??

Ответы [ 2 ]

0 голосов
/ 12 апреля 2011

Сделайте делегирование textFields как себя и добавьте следующий код в textFieldDidBeginEditing и textFieldDidEndEditing.

Определите следующие константы: (константы были установлены для iPad. Настройте их для iphone)

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 302;

Добавьте две переменные в файл .h вашего класса. CGFloat animatedDistance; и UITextField * currentField;

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    currentField = textField;
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];

    CGRect textFieldRect =[self.view.window convertRect:textField.bounds fromView:textField];

    CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
    if (orientation == UIInterfaceOrientationLandscapeLeft )
    {
        textFieldRect.origin.y = textFieldRect.origin.x;
        textFieldRect.size.height = textFieldRect.size.width;
        viewRect.size.height = viewRect.size.width;
    }
    else  if (orientation == UIInterfaceOrientationLandscapeRight )
    {
        textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x;
        textFieldRect.size.height = textFieldRect.size.width;
        viewRect.size.height = viewRect.size.width;
    }
    else   if (orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y;
    }

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)* viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;
    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];

}


- (void)textFieldDidEndEditing:(UITextField *)textField {

    [textField resignFirstResponder];

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

    animatedDistance = 0;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];

    currentField = nil;
}
0 голосов
/ 12 апреля 2011

Используйте UIScrollview, чтобы пользователь мог прокрутить вниз. Вы можете использовать inputAccessoryView для UITextField, чтобы отображать кнопки, такие как next и previous, по щелчку позволяйте вводу переходить к следующему текстовому полю и выделять его.

Посмотрите на этот вопрос и ответы на SO, та же проблема, другое решение.

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