2014-01-21 7 views
0

У меня есть следующий код для перемещения вида вверх и вниз, когда клавиатура появляется в текстовом поле.IOS: высота просмотров уменьшается при выполнении анимации

-(void)textFieldDidBeginEditing:(UITextField *)textField{ 
    NSLog(@"height before animation%f",self.view.frame.size.height); 
    NSLog(@"%f",self.view.frame.size.height); 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [self.view setFrame:CGRectMake(0,-216,320,460)]; 
    [UIView commitAnimations]; 
} 

-(void)textFieldDidEndEditing:(UITextField *)textField{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [self.view setFrame:CGRectMake(0,0,320,460)]; 
    [UIView commitAnimations]; 
    NSLog(@"height after animation %f",self.view.frame.size.height); 
    } 

Вот образец журнала я получаю, когда появится клавиатура, а затем редактирование закончено:

2014-01-21 11:00:51.194 Master-view[456:70b] height before animation 568.000000 
2014-01-21 11:00:53.635 Master-view[456:70b] height after animation 460.000000 

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

Вид движется вверх и спускается слишком без проблем. Также визуально нет никакой разницы. Все элементы, которые были там до того, как двигаться вверх, там, после спуска. Но элементы внизу экрана (за пределами высоты 460.0) не являются неразрешимыми.

+0

вид движения вверх ?? или его не работает? – Yohan

+0

вид движется вверх и спускается тоже без проблем. также визуально нет никакой разницы. Все элементы, которые были там до того, как двигаться вверх, там, после спуска. Но элементы внизу экрана (за пределами высоты 460.0) не взаимодействуют. – user2810114

+0

эй, вы меняете высоту обзора там –

ответ

6
-(void)textFieldDidBeginEditing:(UITextField *)textField{ 
NSLog(@"height before animation%f",self.view.frame.size.height); 
NSLog(@"%f",self.view.frame.size.height); 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; 
[self.view setFrame:CGRectMake(0,-216,320,self.view.frame.size.height)]; 
[UIView commitAnimations]; 

}

-(void)textFieldDidEndEditing:(UITextField *)textField{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; 
[self.view setFrame:CGRectMake(0,0,320,self.view.frame.size.height)]; 
[UIView commitAnimations]; 
NSLog(@"height after animation %f",self.view.frame.size.height); 
} 

Я поставил рамку как на высоте зрения вы меняете только позицию у когда ключ beginediting и попытаться установить его обратно, когда конец редактирование может вам помочь

0

Try изменения как

-(void)textFieldDidBeginEditing:(UITextField *)textField{ 
    NSLog(@"height before animation%f",self.view.frame.size.height); 
    NSLog(@"%f",self.view.frame.size.height); 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y = -216; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations]; 
} 

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

    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y = 0; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations]; 
    NSLog(@"height after animation %f",self.view.frame.size.height); 
} 
0

Мы используем клавиатуру NSNotification, которая будет показывать и брелок d скроет для этого.

мы используем уведомление о добавлении для демонстрации клавиатуры и скрываем, что добавляем в viewWillAppear уведомление и удаляем уведомление в viewWillDisappear.

-(void)viewWillAppear:(BOOL)animated{** 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow) 
               name:UIKeyboardWillShowNotification 
               object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHide) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 
} 

- (void)viewWillDisappear:(BOOL)animated** 
{ 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillShowNotification 
                object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillHideNotification 
                object:nil]; 
} 


-(void)keyboardWillShow { 

    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 



-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 

     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 
Смежные вопросы