2015-06-20 70 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

复制粘贴我的代码并检查出来 –

相关问题