第一次设置后,导航栏外观不会更改

时间:2013-11-04 09:41:52

标签: ios uinavigationbar vertical-alignment uiappearance

当应用变为有效时,我为导航栏的标题设置了一定的垂直偏移:

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

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

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

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

谢谢!

2 个答案:

答案 0 :(得分:1)

来自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];

对于斯威夫特......

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

答案 1 :(得分:0)

您是否尝试过使用动画块?

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