anneblue
anneblue

Reputation: 706

Crash when tapping on UITextField or UITextView: Assertion failure in -[UICalloutBarButton layoutSublayersOfLayer:]

Answers to similar questions did not help so I'm opening a new question.

I have this issue in several of my projects now:

Tapping on the first responder UITextField or on a UITextView (that would normally bring up the default context menu for copy and paste) causes my app(s) to crash.

Note: UICalloutBarButton is a UIKit private API, so there is no chance to change its layoutSubviews implementation and call [super layoutSubviews] in it.

Log message:

*** Assertion failure in -[UICalloutBarButton layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2935.137/UIView.m:8794
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICalloutBarButton's implementation of -layoutSubviews needs to call super.'

I don't have any idea why this appears. Would be very happy for any help.


EDIT

Trying to reproduce the crash in a demo project I found out that it is a custom UIView category that causes the crash. See accepted answer for more details.


Upvotes: 1

Views: 680

Answers (1)

anneblue
anneblue

Reputation: 706

By trying to reproduce the crash in a demo project I found out that it is a custom UIView category that causes the crash.

When creating this fault, I assumed that category methods were only available for UIView subclasses that #import this category.

But this is not the case as described in the Apple Documentation:

Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class. At runtime, there’s no difference between a method added by a category and one that is implemented by the original class.

So not being aware of this, I had overridden in my custom category the UIView method + (BOOL) requiresConstraintBasedLayout to return YES (like I wrongly thought only for those classes that import this category).

Putting a breakpoint in that method, I learned that this 'category-method' gets called from everywhere in the UIKit once the category files are added to a project.

Nothing but a painful way of learning/accepting, that overriding methods in categories is never a good idea - unless you want to cause strange crashes like this ;-).

Sorry for stealing your time, but thank you for asking some code examples which forced me to seriously reproduce the problem - which lead me to the crash-cause...

Upvotes: 1

Related Questions