自定义 UINavigationBar 的错误框架

Bad frame for custom UINavigationBar

我尝试制作自己的自定义 UINavigationBar,但我遇到了一个问题:在我的解决方案中,UINavigationBar 框架的位置错误并且尺寸错误...所以它不通过状态栏。

所以,这是我的自定义导航栏:

import UIKit

class MainNavigationBar: UINavigationBar {

    override func drawRect(rect: CGRect) {
        // not translucent
        translucent = false

        // set background colo
        backgroundColor = UIColor(red: 100/255, green: 169/255, blue: 99/255, alpha: 1)

        // bar style
        barStyle = UIBarStyle.Black

        super.drawRect(rect)
    }
}

我通过一个 UITabBarController 子类加载它(它加载一个 UINavigationViewController 加载我的 UINavigationBar):

override func viewWillAppear(animated: Bool) {
    // init recipe
    let recipeItem = RecipeNavigationViewController(navigationBarClass: MainNavigationBar.self, toolbarClass: nil)

    [...]

    // view controllers displayed by the tab bar interface
    let controllers = [recipeItem, accountItem]
    self.viewControllers = controllers
}

因此使用此代码,我的自定义 UINavigationBar 不会在状态栏下通过。
我能做什么?

谢谢!

我认为您不应该覆盖 UINavigationBar。您尝试自定义的属性已由 UINavigationController 公开,因此您可以使用 self.navigationController.

在 ViewController 中更改它

你真的需要 UINavigationBar 的子类吗? 你不能用 AppearanceProxy 实现同样的事情吗?

UINavigationBar.appearance().titleTextAttributes = [
        NSFontAttributeName: UIFont.XXX,
        NSForegroundColorAttributeName: UIColor.XXX]

    UINavigationBar.appearance().tintColor = .XXX