【问题标题】:UINavigationBar appearance proxy when contained in UIPopoverController包含在 UIPopoverController 中时的 UINavigationBar 外观代理
【发布时间】: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


    【解决方案1】:

    适用于 iOS 8

    NS_CLASS_AVAILABLE_IOS(8_0) @interface UIPopoverPresentationController : UIPresentationController
    

    使用以下内容对我有用。导航控制器包含在 UIPopoverPresentationController 中。

    appearance = [UINavigationBar appearanceWhenContainedIn:[UIPopoverPresentationController class], nil];
    

    适用于 iOS 7

    appearance = [UINavigationBar appearanceWhenContainedIn:[UIPopoverController class], nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.navigationBar.barStyle = UIBarStyleBlack;
    

    如果导航控制器是从storyboard加载的,还需要在storyboard中设置barStyle为UIBarStyleBlack。

    【讨论】:

    • 谢谢。这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2014-03-06
    • 2017-07-05
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多