Skip to content

Instantly share code, notes, and snippets.

@ArtSabintsev
Last active August 31, 2018 16:55
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtSabintsev/8825288 to your computer and use it in GitHub Desktop.
Save ArtSabintsev/8825288 to your computer and use it in GitHub Desktop.
Accessing default view from a UIBarButtonItem
/*
UIBarButtonItem does not subclass UIView.
However, there is a way to access the UIBarButtonItem's default view without breaching
Apple's 'Do not use private APIs' rule.
That's because said view responds to KVO, such as changing the tint-color.
*/
/*
After setting your UIBarButton item inside the UINavigationBar object or UIToolBar object,
access your view in the following fashion:
*/
UIView *view = [self.navigationItem.rightBarButtonItem valueForKey:@"view"]; // My UIBarButtonItem was previously set to the rightBarbuttonItem of a UINavigationBar/UINavigationController.
// As a check, do the following to find out if you get an object back:
NSLog(@"%@", NSStringFromClass([[self.navigationItem.rightBarButtonItem valueForKey:@"view"] class]));
/*
You'll find out that your view is actually an instance or UINavigationButton,
which subclasses UIButton, which itself subclasses UIView.
Now, just to double check to see if it subclasses UIView, do the following:
*/
NSLog(@"%d", [[self.navigationItem.rightBarButtonItem valueForKey:@"view"] isKindOfClass:[UIView class]]);
// So, to edit the exclusiveTouch property, do the following:
UIView *view = [self.navigationItem.rightBarButtonItem valueForKey:@"view"];
view.exclusiveTouch = YES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment