Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `valueForKey` to access view in UIBarButtonItem, private API violation?

Since UIBarButtonItem doesn't subclass UIView, it's impossible to get at the normal characteristics like its frame.

One way to do this is [barButtonItem valueForKey:@"view"]

This works perfectly, and allows you to add a GestureRecognizer (for instance) to the underlying UIView.

However, is this a private UIKit API violation?

like image 717
Dan Rosenstark Avatar asked Aug 12 '12 16:08

Dan Rosenstark


1 Answers

This is not private in terms of immediate rejection upon validation, but it's private enough to be considered fragile (that is, new iOS version can break your existing app in the app store that's using the code).

I can say, that a similar code (fetching backgroundView ivar of UIToolbar via KVC) has passed app store validation and is being used in production.

In case of possible bad things, you must wrap the method in @try { ... } @catch, so that you intercept KVC possibly failing in newer iOS release.

like image 69
Farcaller Avatar answered Oct 17 '22 08:10

Farcaller