【问题标题】:Autolayout in UITextField: Auto Layout still required after executing -layoutSubviewsUITextField 中的自动布局:执行 -layoutSubviews 后仍需要自动布局
【发布时间】:2014-10-23 19:00:54
【问题描述】:

我正在继承 UITextField 以在左侧添加标签。我正在使用自动布局来布局标签。但是,我不断遇到这种崩溃:

这是我的布局代码:

- (void)updateConstraints {

self.segmentLabel.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *constraint;
constraint = [NSLayoutConstraint constraintWithItem:self.segmentLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0f];
[self addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self.segmentLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0f];
[self addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self.segmentLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0f];
[self addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:self.segmentLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:self.segmentWidth];
[self addConstraint:constraint];

[super updateConstraints];

}

当我不对文本字段进行任何调整时,它可以正常工作。

但是,如果我尝试设置占位符文本,则会出现以下异常:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“执行 -layoutSubviews 后仍需要自动布局。 DDSegmentedTextField的-layoutSubviews的实现需要调用super。'

但是,我没有覆盖 -layoutSubviews。

有人遇到过吗?我做错了什么?

谢谢!

【问题讨论】:

  • 显示设置占位符文本的代码。
  • 只是 self.tipField.placeholder = @"placeholder";在控制器的 viewDidLoad 中

标签: ios uitextfield autolayout


【解决方案1】:

所以几天前我也遇到了同样的错误。事实证明,我试图在我的 UITextfield 子类中布局子视图,在它们上设置属性,移动它们等,但从未明确告诉视图自行布局(即调用 [self layoutIfNeeded])。

iOS 8 似乎强制一个视图布局其所有子视图,然后在其上配置约束。 iOS 7 不会,如果您使用自动布局,则需要您明确告诉视图在更改时重绘其子视图。

在我的例子中,我对UITextField 进行了子类化,并在旁边添加了一个标签。我通过向 UITextfield 添加约束来配置标签的框架。我可以在课堂上调用的公共方法之一是

- (void)setLabelText:(NSString *)newText{
    self.sideLabel.text = newText;
}

当包含我的子类文本字段的视图控制器出现时,这导致我的应用程序崩溃。通过添加layoutIfNeeded,现在在 iOS7 和 iOS8 中一切正常。

- (void)setLabelText:(NSString *)newText{
    self.sideLabel.text = newText;
    [self layoutIfNeeded];
}

每次更改子类中的视图的一部分时都需要调用它。这包括添加子视图时的设置,更改视图属性时的设置等等。在更改视图的函数返回之前,请在视图上调用 layoutIfNeeded。这似乎适用于一些标准的 UI 控件,包括 UITextfield、UITableView 和 UICollectionView,尽管我确信还有其他控件。我希望这足够清楚并有助于解决您的问题。

您遇到的错误并不是很有用,甚至不适用于我的情况。虽然我收到了完全相同的错误,但我的视图都没有实现layoutSubviews,因此都使用[super layoutSubviews] 方法。

【讨论】:

    【解决方案2】:

    这个问题出现在 iOS

    在创建 UIView 子类和添加视图时,我们可以指示控件首先构造和自动布局子视图。 当需要更新 UI 时使用此语句:

    [self layoutIfNeeded];

    iOS 8+ 正在内部自行处理该问题。

    【讨论】:

      【解决方案3】:

      我有同样的问题。这可能是由错误的约束引起的,可能是由 Storyboard 中的某些设置引起的。尝试删除其中一些,看看是否能找出问题所在

      【讨论】:

        猜你喜欢
        • 2012-09-18
        • 2015-07-09
        • 2015-11-10
        • 2013-07-28
        • 1970-01-01
        • 2015-01-01
        • 1970-01-01
        • 2013-11-19
        • 1970-01-01
        相关资源
        最近更新 更多