简体   繁体   English

应用程序在自定义视图iOS 7.0中的覆盖layoutSubviews方法上崩溃

[英]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.

相关问题 自定义视图中用于子视图布局的自定义方法,或使用layoutSubviews / layoutIfNeeded - custom method in custom view for subview layout, or use of layoutSubviews/layoutIfNeeded iOS - 自定义视图:在 layoutSubviews() 中更新后忽略固有内容大小 - iOS - Custom view: Intrinsic content size ignored after updated in layoutSubviews() iOS 8自定义标签LayoutSubviews方法永远不会被调用? - iOS 8 Custom label LayoutSubviews method never get called? iOS SIGSEGV SEGV_ACCERR在layoutSubviews中崩溃 - iOS SIGSEGV SEGV_ACCERR crash in layoutSubviews 切换到XCode 7.0(.1)后,发布模式下的自定义视图(XIB)崩溃 - Custom view (XIB) crash in Release mode after switching to XCode 7.0(.1) iOS表格视图单元格动态尺寸-LayoutSubviews - iOS Table View Cell Dynamic Size - LayoutSubviews iOS自定义UIView设计:init与layoutSubviews - iOS custom UIView design: init vs layoutSubviews ios UITableView与自定义容器视图控制器崩溃 - ios UITableView Crash with Custom Container View Controller application:didFinishLaunchingWithOptions:在应用崩溃时被调用的方法 - application:didFinishLaunchingWithOptions: method getting called on app crash Objective c / iOS:如何在 viewController 中覆盖自定义类的 layoutSubviews - Objective c / iOS: How to override custom class's layoutSubviews in viewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM