2015-06-20 136 views
1

當鍵盤顯示時,我的UIView沒有向上移動,並且在隱藏鍵盤時也沒有移動。當鍵盤顯示時UIView不向上移動AutoLayout

這種編碼是在viewDidLoad中

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; 

這個編碼是休息。

- (void)keyboardDidShow:(NSNotification *)note 
{ 
    [self animateTextField:TRUE]; 
} 

- (void)keyboardDidHide:(NSNotification *)note 
{ 
    [self animateTextField:FALSE]; 
} 

-(void)animateTextField :(BOOL)up 
{ 
    const int movementDistance = -130; // tweak as needed 
    const float movementDuration = 1.0; // tweak as needed 

    int movement = (up ? movementDistance : - movementDistance); 

    [UIView beginAnimations: @"animateTextField" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    _buttonsView.frame = CGRectOffset(_buttonsView.frame, 0, movement); 
    [UIView commitAnimations]; 
} 
+0

是你嗎使用自動佈局?如果自動佈局爲ON,則不能直接更改幀或中心 – 0yeoj

+0

@ 0yeoj yap,那麼我使用自動佈局,因此有任何解決方法,請問? – ppshein

+0

@ 0yeoj能發表完整答案嗎? – ppshein

回答

0

使用自動佈局,您可以爲約束常量設置動畫,儘管您不會更改CGRect框架。那是古老的方式。

請參閱此鏈接,我回答了類似的問題,他們有動畫一個UIView

Animate UIView with layoutConstraints

這應該有助於運動的問題,儘管隨時讓我知道如果你需要任何澄清。

+0

你可以發佈完整答案更澄清? – ppshein

+0

@ppshein當然,但如果你可以先告訴我,你有沒有約束添加到UIView?您需要先執行此操作,然後按住CTRL將有關視圖距離的約束之一拖動到屏幕的頂部或底部。 –

+0

它現在正在工作,並從您的答案中得到新的教訓。欣賞它。 – ppshein

1

我覺得這適合您實現:

-(void)animateTextField :(BOOL)up 
{ 
    const int movementDistance = -130; // tweak as needed 
    const float movementDuration = 1.0; // tweak as needed 

    int movement = (moveUp ? movementDistance : - movementDistance); 

    [UIView beginAnimations: @"animateTextField" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    _buttonsView.frame = CGRectOffset(_buttonsView.frame, 0, movement); 
    [UIView commitAnimations]; 

    [_buttonsView setNeedsLayout]; 
} 

編輯

我想你的代碼,並呼籲setNeedsLayout所做的工作..只是下面的代碼

0

使用你不需要從任何地方撥打它

#pragma mark - move view up when keyboard is displayed 


- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    [self animateTextField: textField up: YES]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    [self animateTextField: textField up: NO]; 
} 

- (void) animateTextField: (UITextField*) textField up: (BOOL) up 
{ 
    const int movementDistance = 80; // tweak as needed 
    const float movementDuration = 0.3f; // tweak as needed 

    int movement = (up ? -movementDistance : movementDistance); 

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    contentView.frame = CGRectOffset(contentView.frame, 0, movement); 
    [UIView commitAnimations]; 
} 
+0

與我的編碼相同的工作流程。根本不工作。 – ppshein

+0

你可以先試試 –

+0

複製粘貼我的代碼並檢查出來 –

相關問題