sha*_*sha 1 cocoa-touch objective-c ios uiappearance
我正在做一些应该在iOS7和8中工作的东西,但由于某些原因它没有.我想通过外观代理自定义导航栏属性,并希望它应用于所有导航栏甚至是里面的导航栏UIPopover.
所以,第一步我做以下事情:
UINavigationBar *appearance = [UINavigationBar appearance];
appearance.barTintColor = [UIColor redColor];
appearance.titleTextAttributes = @{
NSForegroundColorAttributeName: [UIColor yellowColor]
};
Run Code Online (Sandbox Code Playgroud)
这应该使所有导航栏红色带黄色标题.适用于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];
Run Code Online (Sandbox Code Playgroud)
好的,所以我决定尝试appearanceWhenContainedIn明确地设置它的外观.在初始外观自定义中添加了以下代码:
appearance = [UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil];
appearance.barTintColor = [UIColor greenColor];
appearance.titleTextAttributes = @{
NSForegroundColorAttributeName: [UIColor blueColor]
};
Run Code Online (Sandbox Code Playgroud)
现在.出于某种原因,最后一个代码不会影响任何内容.在iOS8中,UIPopoverControllers中的导航栏仍然是红色+黄色,而不是绿色+蓝色,而iOS7仍然使用默认外观.
我在这做错了什么?
以下是测试项目的链接:https://dl.dropboxusercontent.com/u/6402890/TestAppearance.zip
适用于iOS 8
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIPopoverPresentationController : UIPresentationController
Run Code Online (Sandbox Code Playgroud)
使用以下工作对我有用.导航控制器包含在UIPopoverPresentationController中.
appearance = [UINavigationBar appearanceWhenContainedIn:[UIPopoverPresentationController class], nil];
Run Code Online (Sandbox Code Playgroud)
适用于iOS 7
appearance = [UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.navigationBar.barStyle = UIBarStyleBlack;
Run Code Online (Sandbox Code Playgroud)
如果从故事板加载导航控制器,则还需要在故事板中将barStyle设置为UIBarStyleBlack.
| 归档时间: |
|
| 查看次数: |
1497 次 |
| 最近记录: |