UINavigationBar 不延伸到状态栏后面

UINavigationBar does not extend behind status bar

我觉得我完全忽略了一些东西,因为这太基础了。

在完全简单的设置中:

window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.backgroundColor = UIColor.whiteColor()

let rootController = MainViewController()
rootNavigationController = UINavigationController(rootViewController: rootController)

window.rootViewController = rootNavigationController;
window.makeKeyAndVisible()

// Appearance
UINavigationBar.appearance().barTintColor = UIColor.DailyRate.blueColor
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().opaque = false

我得到的导航栏不会延伸到状态后面,这应该是默认行为。

我刚刚试过了,我得到了正确的结果。请找到我的完整代码。我在你的代码中找不到一些东西(我不明白你所说的 DailyRate 是什么意思),其余的东西与你的代码相同。

var window: UIWindow?
    var rootNavigationController : UINavigationController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.backgroundColor = UIColor.whiteColor()

    let rootController = ViewController()
    rootNavigationController = UINavigationController(rootViewController: rootController)

    window!.rootViewController = rootNavigationController;
    window!.makeKeyAndVisible()

    // Appearance
    UINavigationBar.appearance().barTintColor = UIColor.blueColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    UINavigationBar.appearance().opaque = false

    // Override point for customization after application launch.
    return true
}

结果在下面的附件中。

原来是时间问题

根层次结构实际上是在名为 UIManager 的单独 class 的初始化程序中设置的。但是,此 class 与 AppDelegate

同时初始化
var uiManager = UIManager()

不是 application(_, didFinishLaunchingWithOptions _) 方法中,因此造成了这种奇怪的情况。

所以我所做的只是

var uiManager: UIManager?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    uiManager = UIManager()

}

现在一切都恢复正常了。

感谢@govindarao-kondala 将正确的想法植入我的脑海!

我遇到这个问题是因为我在去掉导航栏底部的内置线条边框,像这样:

if let navigationBar = self.navigationController?.navigationBar {
    navigationBar.shadowImage = UIImage()
    navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
    navigationBar.backgroundColor = UIColor.redColor()
}

我在我的视图控制器中执行上述代码 viewWillAppear 因为我的一些 VC 的导航栏有其他颜色,我不想修改通用外观。

解决方案是用我想要的颜色创建一个 1 pt x 1 pt 图像并使用它而不是新的空 UIImage 实例,如下所示:

if let navigationBar = self.navigationController?.navigationBar {
    let colorImage = UIImage.imageWithColor(self.category.color)
    navigationBar.shadowImage = colorImage
    navigationBar.setBackgroundImage(colorImage, forBarMetrics: .Default)
    navigationBar.tintColor = UIColor.whiteColor()
}

imageWithColor 是我在 UIImage:

的扩展中定义的函数
class func imageWithColor(color: UIColor) -> UIImage {
    let rect = CGRectMake(0, 0, 1, 1)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()

    CGContextSetFillColorWithColor(context, color.CGColor)
    CGContextFillRect(context, rect)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}