我创建了这个类来配置每个 View Controller 以避免 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")
当我调用函数时,它没有改变任何东西,我做错了吗?
最佳答案
您需要使用您的配置类来扩展您的 View Controller 。
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)
}
}
关于ios - 创建一个具有配置 View 外观功能的类 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28675868/