【问题标题】:Navigation bar appearance doesn't change after a first setting第一次设置后导航栏外观不会改变
【发布时间】:2013-11-14 21:51:12
【问题描述】:

当应用激活时,我为导航栏的标题设置了一定的垂直偏移量:

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-3.0f forBarMetrics:UIBarMetricsDefault];

然后,在导航层次结构的后面,我需要设置不同的垂直偏移量,所以我调用:

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-0.5f forBarMetrics:UIBarMetricsDefault];

但我发现当我在应用程序处于活动状态时导航时不会应用新的垂直偏移量。但是,如果应用程序变为非活动状态然后再次活动,则会应用它。当应用程序保持在前台时,如何更改此偏移量?

谢谢!

【问题讨论】:

    标签: ios uinavigationbar vertical-alignment uiappearance


    【解决方案1】:

    你试过用动画块做这个吗?

     [UIView animateWithDuration:3
                 animations:^{
                              [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-3.0f forBarMetrics:UIBarMetricsDefault];
                              [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-0.5f forBarMetrics:UIBarMetricsDefault];
                         }];
    

    【讨论】:

    • 谢谢,我刚刚尝试过,但都不起作用...可能是因为我在 iOS 7 中进行测试,我必须以不同的方式执行此操作吗?
    【解决方案2】:

    来自https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html

    当视图进入窗口时,iOS 会应用外观更改,但它不会 更改已经在窗口中的视图的外观。 改变 当前在窗口中的视图的外观,删除该视图 从视图层次结构中,然后将其放回。

    所以基本上你需要做一些 HACK 来删除视图,然后立即将其添加回来,就像这样......

    UIView *currentview = ((AppDelegate*)[UIApplication sharedApplication].delegate).window.rootViewController.view;
    UIView *superview = currentview.superview;
    [currentview removeFromSuperview];
    [superview addSubview:currentview];
    

    对于 Swift...

    if let currentview = (UIApplication.sharedApplication().delegate as? AppDelegate)?.window?.rootViewController?.view {
        if let superview = currentview.superview {
            currentview.removeFromSuperview()
            superview.addSubview(currentview)
        }
    }
    

    【讨论】:

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