【发布时间】: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