减小UITextView的大小以显示UIButton

时间:2014-10-07 20:28:33

标签: ios swift uibutton uitextview uianimation

我的应用中有一个文本视图,旁边有一个完成按钮来重新设置键盘。基本上,我没有将完成按钮永久地放在文本视图旁边,而是希望文本视图在轻敲时减小宽度,然后在空间中显示完成按钮。然后,当用户点击屏幕或完成按钮时,按钮消失,文本视图再次增加到正常大小。

我已经实现了在用户开始输入时显示按钮的代码但是我想更改它以便在他们点击文本视图时出现,我还需要帮助添加动画以缩小文本视图

感谢。

1 个答案:

答案 0 :(得分:1)

实施textview的委托方法textViewShouldBeginEditing:。然后在那个方法中,做这样的事情:

button.alpha = 0.0f;
button.hidden = NO;
[UIView animationWithDuration:0.3f animations:^{
    NSInteger padding = 5;
    CGRect textViewFrame = textView.frame;
    textViewFrame.width -= CGRectGetWidth(button.frame) - padding;

    // Edit
    textView.frame = textViewFrame;

    // and just to fade the button in as the text field shrinks
    button.alpha = 1.0;
}];

当你点击按钮将文本字段返回到正确的宽度

时,我会做相反的事情