【问题标题】:UIAppearance subview disappears from NavigationbarUIAppearance 子视图从导航栏中消失
【发布时间】:2014-04-01 12:21:38
【问题描述】:

我正在对带有 appdelegate 外观的导航栏进行一些更改。

这是我的方法:

-(void) setAppearance{
    NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
    [titleBarAttributes setValue:[UIFont fontWithName:@"AvantGarde-ExtraLight" size:18] forKey:NSFontAttributeName];
    [titleBarAttributes setValue:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:6.0/256.0 green:57.0/256.0 blue:84.0/256.0 alpha:1.0]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    int borderSize = 3;
    UIImageView *navBorder = [[UIImageView alloc] initWithFrame:CGRectMake(0,
                                                                           41,
                                                                           320,
                                                                           borderSize)];

    navBorder.image = [UIImage imageNamed:@"energy_line"];

    navBorder.tag = 999;


    [[UINavigationBar appearance] addSubview:navBorder];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

我的 appdelegate 中有一个方法将窗口设置为我的第一个 viewController,当我调用此方法时,我的 navBorder 会从导航栏中删除。我不明白为什么会发生这种情况,没有代码可以更改视图控制器中导航栏中的任何内容。

- (void)rootView
{
    [self.window setRootViewController:initialViewController];
}

【问题讨论】:

    标签: uiview navigationbar uiappearance


    【解决方案1】:

    我在这里回答了几乎相同的问题:https://stackoverflow.com/a/26414437/538491

    但这里有一个总结: 调用 [[UINavigationBar appearance] 返回接收器类的外观代理。 addSubview: 方法未标记为 UI_APPEARANCE_SELECTOR。 UIAppearance 的代理方法的一个主要缺点是很难知道哪些选择器是兼容的。

    您应该抓住导航栏并通过调用此方法在其中添加图像:[self.navigationController.navigationBar addSubview:navBorder] 或者您应该继承UINavigationBar,这为您提供了更大的灵活性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多