自定义UINavigationBar

时间:2015-05-20 22:04:09

标签: ios swift uinavigationbar

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

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

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无法通过状态栏 我该怎么办?

谢谢!

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

    UINavigationBar.appearance().tintColor = .XXX