当键盘显示AutoLayout时,UIView没有向上移动

时间:2015-06-20 16:42:08

标签: ios objective-c uiview

当键盘显示时,我的UIView没有向上移动,当键盘被隐藏时,UIView也消失了。

此编码位于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];
}

3 个答案:

答案 0 :(得分:1)

我认为这适合您的实施:

"message": "(#803) Some of the aliases you requested do not exist: 

修改

我尝试了您的代码并致电-(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]; } 完成了工作..

答案 1 :(得分:0)

使用自动布局,您可以为约束常量设置动画,但不要更改CGRect帧。那是旧方式。

请看这个链接,我回答了一个类似的问题,他们遇到的问题是动画UIView的动作

Animate UIView with layoutConstraints

这应该有所帮助,但如果您需要澄清任何内容,请随时告诉我。

答案 2 :(得分: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];
}