自动布局中的UITextField:自动布局执行-layoutSubviews后仍然需要 [英] Autolayout in UITextField: Auto Layout still required after executing -layoutSubviews

查看:516
本文介绍了自动布局中的UITextField:自动布局执行-layoutSubviews后仍然需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我子类的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:

下面就是我正在做我的布局code:

Here's how I am doing my layout code:

- (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的需要调用超。

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 子类中,布局子视图,它们设置属性,移动它们,等等,但从来没有明确地告诉视图本身布局(即调用 [自layoutIfNeeded] )。

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]).

8的iOS似乎强制以便布局其子视图所有,然后在其上配置的限制。 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 你的看法。这似乎适用于包括的UITextField,UITableView的和UICollectionView几个标准的UI控件,但我敢肯定还有其他的。我希望这是不够清楚,帮您解决问题。

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 ,因而都使用 [超级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后仍然需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆