我正在继承 UITextField 以在左侧添加标签.我正在使用自动布局来布局标签.但是,我不断遇到这种崩溃:
I'm subclassing UITextField to add a label on the left side. I'm using autolayout to layout the label. However, I keep getting this crash:
这是我的布局代码:
- (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];
}
当我不对文本字段进行任何调整时,它可以正常工作.
When I don't make any adjustments to the textfield, this works fine.
但是,如果我尝试设置占位符文本,则会出现以下异常:
However, if I try to set the placeholder text, I get this exception:
由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:执行 -layoutSubviews 后仍需要自动布局.DDSegmentedTextField的-layoutSubviews的实现需要调用super.'
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. DDSegmentedTextField's implementation of -layoutSubviews needs to call super.'
但是,我没有覆盖 -layoutSubviews.
However, I'm not overriding -layoutSubviews.
有人遇到过吗?我做错了什么?
Has anyone encountered this? What am I doing wrong?
谢谢!
所以我几天前也遇到了同样的错误.事实证明我试图在我的 UITextfield
子类中布局子视图,在它们上设置属性,移动它们等,但从未明确告诉视图自行布局(即调用 [selflayoutIfNeeded]
).
So I ran into this same error a few days ago as well. It turns out I was trying to layout subviews inside my UITextfield
subclass, setting properties on them, moving them, etc, but was never explicitly telling the view to lay itself out (i.e. calling [self layoutIfNeeded]
).
iOS 8 似乎强制一个视图布局其所有子视图,然后在其上配置约束.iOS 7 不会,如果您使用自动布局,则需要您明确告诉视图在更改时重绘其子视图.
iOS 8 seems to force to a view to layout all its subviews, and then configures constraints on it. iOS 7 won't, and needs you to explicitly tell views to redraw their subviews when you change if you're using autolayout.
在我的例子中,我对 UITextField
进行了子类化,并在旁边添加了一个标签.我通过向 UITextfield 添加约束来配置标签的框架.我可以在课堂上调用的公共方法之一是
In my case, I had subclassed UITextField
and added a label to the side. I configured the frame of the label by adding constraints to the UITextfield. One of the public methods I could call on my class was
- (void)setLabelText:(NSString *)newText{
self.sideLabel.text = newText;
}
当包含我的子类文本字段的视图控制器出现时,这导致我的应用程序崩溃.通过添加 layoutIfNeeded
现在一切都可以在 iOS7 和 iOS8 中正常运行了.
This caused my application to crash when a view controller appeared containing my subclassed textfield. By adding layoutIfNeeded
everything now works fine in iOS7 and iOS8.
- (void)setLabelText:(NSString *)newText{
self.sideLabel.text = newText;
[self layoutIfNeeded];
}
每次更改子类中的视图的一部分时都需要调用它.这包括添加子视图时的设置、更改视图属性时的设置等.在更改视图的函数返回之前,请在视图上调用 layoutIfNeeded
.这似乎适用于一些标准的 UI 控件,包括 UITextfield、UITableView 和 UICollectionView,尽管我确信还有其他控件.我希望这足够清楚并有助于解决您的问题.
This needs to be called every time you change a part of the view in your subclass. This includes the setup when you add subviews, when you change view properties, anything really. Before the function that's changing your view returns, call layoutIfNeeded
on your view. This seems to apply for a few standard UI controls including UITextfield, UITableView and UICollectionView, though I'm sure there are others. I hope this was clear enough and helped solve your problem.
您遇到的错误并不是很有用,甚至不适用于我的情况.虽然我收到了完全相同的错误,但我的视图都没有实现 layoutSubviews
,因此都使用了 [super layoutSubviews]
方法.
The error you're getting isn't super useful, and didn't even apply in my case. Though I was receiving the exact same error, none of my views implementing layoutSubviews
, and thus were all using the [super layoutSubviews]
method.
这篇关于UITextField 中的自动布局:执行 -layoutSubviews 后仍需要自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!