2014-09-23 71 views
1

我有一个固定在我的主UIViewController的视图(self.view)底部的文本字段,当用户点击它时,这个函数被调用(通过UIKeyboardWillShowNotification),这将改变高度 - 它只是出现在iOS动画主UIViewController的视图高度

-(void)keyboardWillShow: (NSNotification*) notification { 
    NSDictionary* info = [notification userInfo]; 
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    CGFloat textFieldHeight = activeField.frame.size.height; 
    CGPoint textFieldOrigin = activeField.frame.origin; 
    textFieldOrigin.y += textFieldHeight; 

    CGRect visibleRect = self.view.frame; 
    visibleRect.size.height -= keyboardSize.height; 

    if (!CGRectContainsPoint(visibleRect, textFieldOrigin)) { 
     CGRect r = self.view.frame; 
     r.size.height -= keyboardSize.height; 

     [UIView animateWithDuration:1.0 
        animations:^{ 
         self.view.frame = r; 
        }]; 
    } 
} 

它会调整的self.view.frame很好,所以它是所有被调用和运行,但由于某些原因拒绝了第二动画吧:self.view.frame的立即放置。

我需要做什么才能让这种高度变化动画?

+0

您使用的自动布局? – 2014-09-23 13:53:37

+0

我这么认为。如果这就是你的意思,我将textfield固定在UIViewController视图的底部,左侧和右侧? – 2014-09-23 14:18:08

回答

1

如果您有自动布局,则不应直接更改框架,而应更改约束条件。 添加一个IBOutlet的约束(底部约束),并改变常数像这样:

myTextFieldConstrain.constant -= YOUR_VALUE 

另外,如果你想让它动画,来电[YOURSUPERVIEW layoutIfNeeded];你改变常数后。

例子:

[UIView animateWithDuration:1.0 
        animations:^{ 
         myTextFieldConstrain.constant -= YOUR_VALUE; 
         [YOURSUPERVIEW layoutIfNeeded]; 
        }]; 
1

尝试稍微延迟调度它,如:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 
    [UIView animateWithDuration:1.0 
        animations:^{ 
         self.view.frame = r; 
        }]; 
} 

这通常使的伎俩。

+0

感谢您的回复。我试过这个,它在1秒后出现,即仍然没有动画。我已将动画持续时间延长至10秒,并立即调整大小。 – 2014-09-23 13:50:51