我相信我已经为这个问题提出了目前可用的最有效的解决方案。不幸的是,Youtube 视频播放器属于一个名为MPInlineVideoViewController
. 在这个类上使用外观代理是不可能的,这无论如何都是一种黑客行为。
这是我想出的。我以一种可以在多个地方使用它的方式对其进行编码,并且还可以用于解决其他外观代理问题,例如在 UIWebView 中填写表单时的后面和下一个 UIBarButtonItems。
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];
}
在上面的代码中,我们将外观样式切换回其默认值,因此当 YouTube 播放器加载时,它使用默认样式。当前的 ViewController 已经加载,因此它将具有样式化的外观。
当 YouTube 播放器关闭时,不会重新加载当前的 ViewController,从而保持样式。当当前 ViewController 消失时,样式化的外观会重新打开。