Как сделать прокрутку экрана при использовании клавиатуры iPhone - PullRequest
1 голос
/ 25 июля 2011

Я работаю над приложением в альбомной ориентации. Я использую несколько UITextFields, которые при двойном касании дадут вам возможность редактировать TextFields. У меня вопрос: как заставить экран прокручиваться, чтобы пользователь мог редактировать весь экран, пока отображается клавиатура?

Ответы [ 3 ]

0 голосов
/ 25 июля 2011

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

- (void)scrollViewToCenterOfScreen:(UIView *)theView {
    CGFloat viewCenterY = theView.center.y;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    CGRect keyboardBounds = CGRectMake(0, 280, 320, 200);
    CGFloat availableHeight = applicationFrame.size.height - keyboardBounds.size.height;    // Remove area covered by keyboard

    CGFloat y = viewCenterY - availableHeight / 2.0;
    if (y < 0) {
        y = 0;
    }
    scrollview.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);
    [scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
0 голосов
/ 25 июля 2011

Использование contentInset и scrollRectToVisible хорошо мне помогло.Приведенный ниже код вставляет представление прокрутки, чтобы оно не закрывалось клавиатурой, а затем прокручивает содержимое для отображения текстового поля.

- (void)keyboardWillShow:(NSNotification *)aNotification 
{
    CGRect kbFrame;
    [[aNotification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&kbFrame];                    
    float kbHeight = [self convertRect:kbFrame fromView:nil].size.height;               
    float d = kbHeight - self.frame.origin.y / self.transform.a;        
    d = d < 0 ? 0 : d;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, d, 0.0);

    self.contentInset = contentInsets;
    self.scrollIndicatorInsets = contentInsets;

    UIView *responder = /* ... your text field ... */
    [self scrollRectToVisible:responder.frame animated:YES];        
    [self performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:0.0];
}

- (void)keyboardWillHide:(NSNotification *)aNotification 
{
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;

    [[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

    [UIView animateWithDuration:animationDuration 
                          delay:0 
                        options:animationCurve
                     animations:^{
                         self.contentInset = UIEdgeInsetsZero;
                         self.scrollIndicatorInsets = UIEdgeInsetsZero;                      
                     } 
                     completion:nil];

    [self setContentOffset:CGPointMake(0, 0) animated:YES]; 
    self.scrollEnabled = NO;
}
0 голосов
/ 25 июля 2011

Вы можете использовать метод делегата uitextfield.

`- (void)textFieldDidBeginEditing:(UITextField *)textField
{
            [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    self.view.center=CGPointMake(self.view.center.x, self.view.center.y+60);
    [UIView commitAnimations];

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
            [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    self.view.center=CGPointMake(self.view.center.x, self.view.center.y-60);
    [UIView commitAnimations];

} `

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