[英]Application getting crash on overriding layoutSubviews method in custom view iOS 7.0
I am creating a customize view and need to update its subview frame on orientation change. 我正在创建一个自定义视图,并且需要在方向更改时更新其子视图框架。 To accomplish this I am overriding layoutSubviews in parent view.
为此,我要在父视图中覆盖layoutSubviews。
In layoutSubviews, I am removing previous constraint and adding new constraints to its subview. 在layoutSubviews中,我要删除以前的约束,并为其子视图添加新的约束。
I am calling [super layoutSubviews];
我打电话给
[super layoutSubviews];
first in overriden method. 覆盖方法中的first。 It is working fine in iOS8 onwards but getting crash on iOS 7.0 and below iOS8.
它在iOS8及更高版本上正常运行,但在iOS 7.0及更低版本的iOS8上崩溃。 Getting following message in log
在日志中获取以下消息
Terminated due to uncaught exception 'NSInternalInconsistencyException' reason Auto layout still required after executing layoutSubviews. Parent view's layoutSubviews needs to call super.
If I call [super layoutSubviews];
如果我叫
[super layoutSubviews];
after modifying the constraints ie at last in layoutSubviews, it's working in iOS7 and iOS8 both. 修改约束后,即最后在layoutSubviews中,它在iOS7和iOS8中都可以使用。
My question is what is the correct way to override layoutSubviews. 我的问题是重写layoutSubviews的正确方法是什么。
Am I doing something wrong? 难道我做错了什么?
Should we add constraint on subview in layoutSubviews. 我们是否应该在layoutSubviews中的子视图上添加约束。 or Is there any more better approach to achieve the same output.
还是有其他更好的方法来实现相同的输出。
You are not doing it wrong I think. 我认为您没有做错事情。 As it says in error, it needs auto-layout constraints when it tries to layout it's subviews.
就像错误地指出的那样,当它尝试对其子视图进行布局时,需要自动布局约束。 So before you call
[super layoutSubviews]
you should handle constraint stuff. 因此,在调用
[super layoutSubviews]
,应先处理约束条件。
If I call [super layoutSubviews];
如果我叫[super layoutSubviews]; after modifying the constraints... it's working in iOS7 and iOS8 both.
修改约束后...在iOS7和iOS8中都可以使用。
That is the correct approach - you should modify constraints before you call [super layoutSubviews];
那是正确的方法-您应该在调用
[super layoutSubviews];
之前修改约束[super layoutSubviews];
. 。 That way AutoLayout takes into account your updated constraints.
这样,“自动版式”将考虑您更新的约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.