我有一个 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.
如您所见,上面的 textField 具有正确的大小.下面的 textField 以空文本开头,由于是空文本,它的宽度为 80 磅.当我在编辑 textField 中输入文本时,文本会向左滚动,但不会改变其宽度.
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.
在我看来,这应该是开箱即用的.通过为 UIControlEventEditingChanged
事件实现 IBAction
,我可以确认键入实际上会更改 UITextField 的 intrinsicContentSize
.
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.
但是,在 textField 不再是第一响应者之前,宽度不会改变.如果我将光标放到另一个 textField 中,则设置已编辑 textField 的宽度.这对我想要的有点晚了.
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?
这对我有用:
- (IBAction) textFieldDidChange: (UITextField*) textField
{
[UIView animateWithDuration:0.1 animations:^{
[textField 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.
一堆这对我不起作用".我认为这里的混乱可能是与 textFieldDidChange:
处理程序相关的触发事件.该事件需要是 UIControlEventEditingChanged.如果您使用的是 IB,请仔细检查您处理的事件是否正确.
A bunch of "This is not working for me". I think the confusion here is perhaps the triggering event that is tied to the textFieldDidChange:
handler. The event needs to be UIControlEventEditingChanged. If you're using IB, double check that you're handling the right event.
UITextField
也不能限制大小.您可以使用约束将其锁定到位,但任何宽度约束或一组左右定位约束都会阻止它调整到其内在内容大小.
The UITextField
also cannot be constrained in size. You can lock it into position with constraints, but any width constraint, or set of left+right positioning constraints will prevent it from resizing to its intrinsic content size.
这篇关于键入时调整 UITextField 的大小(使用自动布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!