如何在开始编辑时拉伸UITextField

时间:2011-07-21 07:45:23

标签: iphone objective-c

我希望在编辑开始时增加UITextField的大小,但是无法获得代码来执行此操作。

2 个答案:

答案 0 :(得分:1)

实施UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField中,将新帧大小设置为文本字段。然后拨打[textField setNeedsDisplay];

答案 1 :(得分:1)

Gomathi是对的。如果您想在编辑开始时扩展textFIeld并在编辑结束时再次收缩并且想要为其设置动画,则只需添加到他的答案中,您可能想要执行以下操作:

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [UIView animateWithDuration:0.5 animations:^{
        textField.frame = CGRectMake(textField.frame.origin.x-20, textField.frame.origin.y, textField.frame.size.width+40, textField.frame.size.height);
    }];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [UIView animateWithDuration:0.5 animations:^{
        textField.frame = CGRectMake(textField.frame.origin.x+20, textField.frame.origin.y, textField.frame.size.width-40, textField.frame.size.height);
    }];
    [textField resignFirstResponder];
    return YES;
}