我创建了这个类来配置每个视图控制器以避免viewdidload()中的冗余。
class Configuration: UIViewController {
func setNavigationTheme(#backgroundImage: String, dashboardImage: String) {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: dashboardImage), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
let bgImage: UIImage = UIImage(named: backgroundImage)!
self.view.backgroundColor = UIColor(patternImage: bgImage)
}
}
在viewDidLoad()
中var configure = Configuration()
configure.setNavigationTheme(backgroundImage: "Background", dashboardImage: "Dashboard")
当我调用该函数时,它不会改变任何东西,我是否做错了?
答案 0 :(得分:1)
您需要使用Configuration类扩展视图控制器。
class MyViewController: Configuration {
override func viewDidLoad() {
//Don't initialize new Configuration class.
setNavigationTheme(backgroundImage: "Background", dashboardImage: "Dashboard")
}
}
您的配置类:
class Configuration: UITableViewController {
func setNavigationTheme(#backgroundImage: String, dashboardImage: String) {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: dashboardImage), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
let bgImage: UIImage = UIImage(named: backgroundImage)!
self.view.backgroundColor = UIColor(patternImage: bgImage)
}
}