简体   繁体   中英

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.

In layoutSubviews, I am removing previous constraint and adding new constraints to its subview.

I am calling [super layoutSubviews]; first in overriden method. It is working fine in iOS8 onwards but getting crash on iOS 7.0 and below 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]; after modifying the constraints ie at last in layoutSubviews, it's working in iOS7 and iOS8 both.

My question is what is the correct way to override layoutSubviews.

Am I doing something wrong?

Should we add constraint on subview in 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.

If I call [super layoutSubviews]; after modifying the constraints... it's working in iOS7 and iOS8 both.

That is the correct approach - you should modify constraints before you call [super layoutSubviews]; . That way AutoLayout takes into account your updated constraints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM