ios - 创建一个具有配置 View 外观功能的类 - swift

标签 ios swift

我创建了这个类来配置每个 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/

相关文章:

ios - CoreData 子上下文、NSFetchedResultsController 和主线程

ios - 在 UITableViewCell 中播放视频的最佳方式是什么

ios - Swift 代码中的 UICollectionView?为什么我的代码不起作用?

ios - Sqlite.swift 实时搜索

ios - 基本 while 循环中索引超出范围

ios - 布局相对于边距警告 XCode 6 但没有约束

ios - 更改 CNContactPicker 中取消和完成按钮的颜色

ios - 如何更改类的属性而不必使用 Realm 删除应用程序

ios - SwiftUI 双向绑定(bind)到枚举案例中 ObservableObject 中的值

ios - 如何删除其中有空格的键? ( swift )