调整为UITextField打字时(通过使用自动布局) [英] Resize a UITextField while typing (by using Autolayout)
问题描述
我有一个UITableViewCell有两个UITextFields(无国界)。以下约束用于设置水平布局。
I have a UITableViewCell which has two UITextFields (without borders). The following constraints are used to set up the horizontal layout.
@| -10- [leftTextField(大于= 80)] - (大于= 10) - [rightTextField(大于= 40)] - 10 |
果然没什么特别的,和作品。
Nothing fancy, and works as expected.
正如你可以看到文本框上有正确的尺寸。较低的文本框开始与一个空的文本,因为空的文本是80点宽。当我键入文本,编辑文本框文本滚动到左边,它不会改变它的宽度。
As you can see the upper textField has the correct size. The lower textField started with an empty text, and because of the empty text it is 80 points wide. When I type text into the editing textField the text scrolls to the left, it does not change its width.
我不喜欢的是,文本框的宽度应在用户类型调整到该文本框。
I don't like that, the width of the textField should adjust while the user types into that textField.
在我看来,应该工作的开箱。通过实施 IBAction为
为 UIControlEventEditingChanged
事件中,我可以证实,打字实际上改变了 intrinsicContentSize
中的UITextField的。
In my opinion that should work out of the box. By implementing a IBAction
for the UIControlEventEditingChanged
event I can confirm that typing actually changes the intrinsicContentSize
of the UITextField.
但好,宽度不改变,直到文本框不再是第一个响应者。如果我把光标移动到另一个文本框的文本字段编辑的宽度设置。那是因为我想要的东西有点晚了。
But well, the width does not change until the textField is no longer the first responder. If I put the cursor into another textField the width of the edited textField is set. That's a bit late for what I want.
这些注释行的是什么我想没有任何成功:
These commented out lines is what I tried without any success:
- (IBAction)textFieldDidChange:(UITextField *)textField {
[UIView animateWithDuration:0.1 animations:^{
// [self.contentView removeConstraints:horizontalConstraints];
// [self.contentView addConstraints:horizontalConstraints];
// [self.contentView layoutIfNeeded];
// [self.contentView setNeedsLayout];
// [self.contentView setNeedsUpdateConstraints];
}];
NSLog(@"%@", NSStringFromCGSize(textField.intrinsicContentSize));
}
有谁知道我缺少的是什么?我应该怎么做,以使这项工作?
Does anybody know what I am missing? What could I try to make this work?
推荐答案
这对我来说是什么作品:
This is what works for me:
- (IBAction) textFieldDidChange: (UITextField*) textField
{
[UIView animateWithDuration:0.1 animations:^{
[textField invalidateIntrinsicContentSize];
}];
}
有趣的是,它好像它应该工作的开箱,鉴于这种<一个href=\"http://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Articles/dividingResponsibility.html\">text从文档:
此外,如果一个视图更改属性,并改变影响
内在内容大小,视图必须调用
invalidateIntrinsicContentSize使布局系统的通告
改变,可以重新布局。在您的视图类的实现,
你必须确保如果任何属性的值赖以
内在的大小取决于变化,可以调用
invalidateIntrinsicContentSize。 例如,文本字段通话
invalidateIntrinsicContentSize如果字符串值的变化。
In addition, if a property of a view changes and that change affects the intrinsic content size, the view must call invalidateIntrinsicContentSize so that the layout system notices the change and can re-layout. In the implementation of your view class, you must ensure that if the value of any property upon which the intrinsic size depends changes, you invoke invalidateIntrinsicContentSize. For example, a text field calls invalidateIntrinsicContentSize if the string value changes.
我最好的猜测是,一旦编辑完成后,不会在文本字段只要求invalidateIntrinsicContentSize。
My best guess is that the textfield only calls invalidateIntrinsicContentSize once editing has finished, not during.
这篇关于调整为UITextField打字时(通过使用自动布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!