【发布时间】:2014-10-23 20:54:13
【问题描述】:
我正在做一些应该在 iOS7 和 8 中工作的事情,但由于某种原因它没有。我想通过外观代理自定义导航栏属性,并希望将其应用于所有导航栏,甚至是UIPopover 内的导航栏。
所以,第一步我做以下事情:
UINavigationBar *appearance = [UINavigationBar appearance];
appearance.barTintColor = [UIColor redColor];
appearance.titleTextAttributes = @{
NSForegroundColorAttributeName: [UIColor yellowColor]
};
这应该使所有导航栏都变成红色并带有黄色标题。在 iOS8 中工作。主要在iOS7中工作。由于某种原因,当视图控制器出现在 UIPopoverController 中时 - 它会获得默认外观。
这就是我呈现 popover 的方式(没什么花哨的 - 几乎是标准示例代码):
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"];
vc.title = @"View Controller 2";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.popover = [[UIPopoverController alloc] initWithContentViewController:nav];
[self.popover presentPopoverFromRect:CGRectMake(100, 100, 100, 100) inView:self.view permittedArrowDirections:0 animated:YES];
好的,所以我决定尝试appearanceWhenContainedIn 并在那里明确设置它的外观。在初始外观自定义中添加了以下代码:
appearance = [UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil];
appearance.barTintColor = [UIColor greenColor];
appearance.titleTextAttributes = @{
NSForegroundColorAttributeName: [UIColor blueColor]
};
现在。由于某种原因,最后一个代码不会影响任何东西。 iOS8中UIPopoverControllers里面的导航栏依然是红+黄,不是绿+蓝,iOS7依然使用默认外观。
我在这里做错了什么?
这里是测试项目的链接:https://dl.dropboxusercontent.com/u/6402890/TestAppearance.zip
【问题讨论】:
标签: ios objective-c cocoa-touch uiappearance