cho*_*ise 7 iphone objective-c uinavigationbar ios5 uiappearance
我UIBarButtonItem使用Appearance API设置了如下样式
[[UIBarButtonItem appearance] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
这适用于整个应用程序.问题是,如果您点击一个YouTube视频,这也会改变加载的YouTube视频的视频视图中的按钮uiwebview.

添加这样的代码:
[[UIBarButtonItem appearanceWhenContainedIn:[MPMoviePlayerViewController class], nil] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
不会改变任何东西(因为看起来YouTube的东西不仅仅是一个MPMoviePlayerViewController.
如果我理解正确,我也不允许更改YouTube视图的按钮(我也不想这样做).
有关如何停止在此YouTube视频观看中设置自定义栏按钮图像的任何想法?
如果您想仔细观察,请参阅示例项目:https://dl.dropbox.com/u/80699/BarItemsSample.zip
因为你误解了什么剂量的外观,当时所包含的:做.
SDK文档说:
要在容器类的实例或层次结构中的实例中包含类的实例的外观,请使用appearanceWhenContainedIn:来获取类的外观代理.
下面的代码就是你在问题中要求的代码.请在问我之前试一试.
对于iOS 5.x,您应该创建UINavigationBar的子类(例如,不需要任何覆盖)
//In MJAppDelegate.h:
@interface MyNavigationBar : UINavigationBar
@end
//In MJAppDelegate.m:
@implementation MyNavigationBar
@end
Run Code Online (Sandbox Code Playgroud)
然后你应该编辑你的故事板,让它使用MyNavigationBar作为它的UINavigationController的导航栏.
最后,您可以使用以下代码获得您想要的内容:
[[UIBarButtonItem appearanceWhenContainedIn:[MyNavigationBar class], nil] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
对于iOS 6,您只需使用以下代码:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], [UINavigationController class], nil] setBackgroundImage:barButtonBgImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)
我相信我已经针对这个问题提出了目前最有效的解决方案。不幸的是,Youtube 视频播放器属于一个名为 的私有类MPInlineVideoViewController。不可能在此类上使用外观代理,无论如何这都是一种黑客行为。
这是我想出的。我以一种可以在多个地方使用它的方式对其进行编码,并且还可以用于解决其他外观代理问题,例如在 UIWebView 中填写表单时的后退和下一个 UIBarButtonItems。
AppDelegate.h
extern NSString * const ToggleAppearanceStyles;
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我们将外观样式切换回默认值,以便在加载 YouTube 播放器时,它使用默认样式。当前的 ViewController 已经加载,因此它将具有样式外观。
当 YouTube 播放器关闭时,当前的 ViewController 将不会重新加载,从而保持样式。当当前 ViewController 消失时,样式化外观将重新打开。