I believe I have come up with the most efficient solution currently available for this problem. Unfortunately the Youtube video player is of a private class called MPInlineVideoViewController
. It is not possible to use the appearance proxy on this class, which would kind of be a hack anyway.
Here is what I came up with. I coded it in a way that it could be used in more than one places and could also be used to solve other Appearance proxy issues, such as the back and next UIBarButtonItems when filling out a form in a UIWebView.
AppDelegate.h
extern NSString * const ToggleAppearanceStyles;
AppDelegate.m
NSString * const ToggleAppearanceStyles = @"ToggleAppearanceStyles";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSNotification *note = [NSNotification notificationWithName:ToggleAppearanceStyles object:NULL userInfo:@{@"flag" : @(YES)}];
[self toggleAppearanceStyles:note];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toggleAppearanceStyles:) name:ToggleAppearanceStyles object:NULL];
return YES;
}
-(void)toggleAppearanceStyles:(NSNotification *)note {
UIImage *barButtonBgImage = nil;
UIImage *barButtonBgImageActive = nil;
if([note.userInfo[@"flag"] boolValue]) {
barButtonBgImage = [[UIImage imageNamed:@"g_barbutton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 4, 15, 4)];
barButtonBgImageActive = [[UIImage imageNamed:@"g_barbutton_active.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 4, 15, 4)];
}
[[UIBarButtonItem appearance] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonBgImageActive forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
}
MJWebViewController.m
-(void)viewDidAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] postNotificationName:ToggleAppearanceStyles object:NULL userInfo:@{@"flag" : @(NO)}];
[super viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] postNotificationName:ToggleAppearanceStyles object:NULL userInfo:@{@"flag" : @(YES)}];
[super viewWillDisappear:animated];
}
In the above code we toggle the appearance styles back to their default values so when the YouTube player is loaded, it uses the default styles. The current ViewController has already loaded so it will have the styled appearance.
When the YouTube player dismisses, the current ViewController will not be reloaded, thus maintaining the styling. When the current ViewController disappears, the styled appearances are turned back on.