2015-05-20 65 views
0

我试图做我自己的自定义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子类加载它(它加载,加载我UINavigationBar一个UINavigationViewController):

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没有通过状态栏。
我该怎么办?

谢谢!

回答

0

我不认为你应该重写UINavigationBar。您试图自定义的属性已经被UINavigationController公开,所以您可以使用self.navigationController在您的ViewController中更改该属性。

+0

谢谢!我还不知道好的做法,但我会创建一个'BaseNavigationViewController'(其中一个我将使用'self.navigationBar')其他'NavigationController'(一个用于我的MainTabViewController'的每个Tab)继承 – Yannis

0

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

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

    UINavigationBar.appearance().tintColor = .XXX 
相关问题