我的 View 中有一个 UITextField,它有大小限制来布置它的位置。我想要发生的是当用户开始编辑字段时 UITextField 的宽度发生变化。
我将 View Controller 设置为该字段的委托(delegate),并且委托(delegate)方法在这里工作
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"Search Field is being edited");
}
我需要知道的是,我需要对 textField
做些什么才能改变它的宽度,因为我尝试过的所有方法都不起作用。
谢谢
最佳答案
您可以做的是为文本字段的宽度约束设置一个导出。 在下面的例子中,我假设它的名字是 textFieldWidthConstraint
在您的 textFieldDidBeginEditing:
方法中添加:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
// no animation
self.textFieldWidthConstraint.constant = newWidth;
// animation
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.3 animations:^
{
self.textFieldWidthConstraint.constant = newWidth;
[self.view layoutIfNeeded];
}
}
你可以对高度做同样的事情。
希望对您有所帮助,如果您需要更多帮助,请告诉我。
关于ios - 在编辑时调整 UITextField 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28744036/