CustomTableCellView's implementation of -layou

2019-02-21 19:14发布

问题:

On using autoLayout in custom cell for tableView xib, i am getting following error.

When running in iOS 6 simulator CustomCells's implementation of -layoutSubviews needs to call super

Assertion failure in 

-[CustomCells layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView
     *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. CustomCells's implementation of -layoutSubviews needs to call super.'
    *** First throw call stack:

When running in iOS 5 simulator

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The NIB data is invalid.'
*** First throw call stack:

Temp Fix :

It is working fine if am disabling the autoLayout in custom cell. But orientation handling is not fine in this case. Please help.

回答1:

layoutSubviews needs to call super

Can be worked around on iOS6 by putting everything in your custom tableview cell in a container view. ie Create a new view to fill the cell then place controls etc inside that. Also make sure you set:

[theContainerView setTranslatesAutoresizingMaskIntoConstraints:NO]; 

for the container and subviews



回答2:

iOS 5.1 and earlier versions are not compatible with AutoLayout. It's only for iOS 6.0+.

Assuming you're using storyboards, if you want to use AutoLayout but still preserve compatibility with iOS versions prior to 6.0, you'll need to create two targets: one for iOS 6 and another for iOS 5 (or earlier). For each, set a separate storyboard, one where AutoLayout is enabled (for iOS 6), and one where it is not (for iOS 5).

Here's a StackOverflow thread for best practices with AutoLayout and backward-compatibility with iOS 5: Enabling auto layout in iOS 6 while remaining backwards compatible with iOS 5.

In my experience, it's not worth the effort to use AutoLayout if you want backwards compatibility. If you're using storyboards, it's a maintenance headache to have 2 targets and 2 storyboards to synchronize. If you're not using storyboards, you'll need separate code for iOS 5 and iOS 6, which will take longer to test and update.