Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appearance API - UIBarButtonItem - MPMoviePlayerViewController and Youtube Webview

I styled a UIBarButtonItem using Appearance API like the following

[[UIBarButtonItem appearance] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

This works great all over the app. The problem is, that this also changes the buttons in video views of YouTube videos that get loaded if you click a YouTube video in a uiwebview.

YouTube Examples

adding code like this:

[[UIBarButtonItem appearanceWhenContainedIn:[MPMoviePlayerViewController class], nil] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

doesn't change anything (because it seems the YouTube thing isn't just a MPMoviePlayerViewController.

If I understand correctly, I am also not allowed to change the buttons of the YouTube view (and also I don't want this).

Any ideas how I could stop setting custom bar button images on this YouTube video view?

Here is the sample project if you want to take a closer look: https://dl.dropbox.com/u/80699/BarItemsSample.zip

like image 540
choise Avatar asked Sep 05 '12 21:09

choise


1 Answers

Because you are misunderstanding what dose appearanceWhenContainedIn: do.

The SDK document said:

To customize the appearances for instances of a class when contained within an instance of a container class, or instances in a hierarchy, you use appearanceWhenContainedIn: to get the appearance proxy for the class.

The code below dose what you are requiring in you question. Try it before you question me please.

For iOS 5.x, you should make a subclass of UINavigationBar (no any override needed), for example

//In MJAppDelegate.h:
@interface MyNavigationBar : UINavigationBar
@end

//In MJAppDelegate.m:
@implementation MyNavigationBar
@end

Then you should edit your storyboard, let it use MyNavigationBar as its UINavigationController's navigation bar.

Finally, you can use the code below to get what you want:

[[UIBarButtonItem  appearanceWhenContainedIn:[MyNavigationBar class], nil] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

For iOS 6, you can just use the code below:

[[UIBarButtonItem  appearanceWhenContainedIn:[UINavigationBar class], [UINavigationController class], nil] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
like image 66
xuzhe Avatar answered Nov 09 '22 10:11

xuzhe