UINavigationBar不会延伸到状态栏后面

时间:2015-11-28 10:00:49

标签: ios uinavigationbar uistatusbar

我觉得我完全忽略了某些东西,因为这是非常基本的。

完全裸骨架设置:

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

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

enter image description here

3 个答案:

答案 0 :(得分:1)

我刚试过,但得到了正确的结果。请找到我的完整代码。我在代码中找不到一些东西(我不明白你的意思是 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
}

结果是以下附件。 enter image description here

答案 1 :(得分:1)

我遇到了这个问题,因为我删除了导航栏底部的内置线条边框,如下所示:

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
}

答案 2 :(得分:0)

原来这是一个时间问题。

根层次结构实际上是在名为UIManager的单独类的初始值设定项中设置的。但是,此类与AppDelegate

同时初始化
var uiManager = UIManager()
application(_, didFinishLaunchingWithOptions _)方法中的

,从而创建了这种奇怪的场景。

所以我所做的就是

var uiManager: UIManager?

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

    uiManager = UIManager()

}

现在一切都恢复正常了。

感谢@ govindarao-kondala在我脑海里种下正确的想法!