Ios UITableView和UITextField边界颜色问题

Ios UITableView和UITextField边界颜色问题,ios,swift,uitableview,uitextfield,Ios,Swift,Uitableview,Uitextfield,我不熟悉斯威夫特,在实践中学习更多。无法确定我的错误。 无论我是编码错误还是实现错误 我正试图在框架的帮助下实现一个主题化特性。 我已经能够更改一些UI元素的颜色,例如UISegmentedControl和UIButton,但是我无法更改UITextField的borderColor和UITableView的borderColor以及分隔色。UITextField和UITableView的颜色更改在相应组件的层上完成。 图层可能是我的问题吗 预览: 第一屏:ViewController 第二屏

我不熟悉斯威夫特,在实践中学习更多。无法确定我的错误。 无论我是编码错误还是实现错误

我正试图在框架的帮助下实现一个主题化特性。 我已经能够更改一些UI元素的颜色,例如
UISegmentedControl
UIButton
,但是我无法更改
UITextField
borderColor
UITableView
borderColor以及分隔色。
UITextField
UITableView
的颜色更改在相应组件的层上完成。 图层可能是我的问题吗

预览:

  • 第一屏:
    ViewController
  • 第二屏:
    ColorTableViewController
  • 我的代码: //ViewController.swift

    import UIKit
    import ChameleonFramework
    import QuartzCore
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var segmentedControl: UISegmentedControl!
    
        @IBOutlet weak var tableView: UITableView!
        @IBOutlet weak var textField: UITextField!
    
        @IBOutlet weak var button: UIButton!
    
        var themeColor = UIColor.black {
            didSet{
                if (UserDefaults.standard.value(forKey: "themeColor") != nil) {
                    themeColor = UserDefaults.standard.value(forKey: "themeColor") as! UIColor
                }
                else{
                    themeColor = UIColor.black
                }
            }
        }
        var themeContrastColor = UIColor.white {
            didSet{
                if (UserDefaults.standard.value(forKey: "contrastThemeColor") != nil) {
                    themeContrastColor = UserDefaults.standard.value(forKey: "contrastThemeColor") as! UIColor
                }
                else{
                    themeContrastColor = UIColor.white
                }
            }
        }
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            styleUI()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func styleUI(){
            DispatchQueue.main.async {
                self.segmentedControl.tintColor = self.themeColor
    
                self.tableView.tintColor = self.themeColor
                self.tableView.layer.borderWidth = 1.0
                self.tableView.layer.borderColor = self.themeColor.cgColor
                self.tableView.separatorColor = self.themeColor
    
                self.textField.layer.borderWidth = 1.0
                self.textField.layer.masksToBounds = true
                self.textField.layer.borderColor = self.themeColor.cgColor
                self.textField.tintColor = self.themeColor
    
                self.button.backgroundColor = self.themeColor
                self.button.tintColor = self.themeContrastColor
            }
    
    
        }
    
    
    }
    extension ViewController: UITableViewDelegate, UITableViewDataSource{
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 7
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) 
            cell.textLabel?.text = "Row: \(indexPath.row)"
            return cell
        }
    
    }
    
    import UIKit
    import ChameleonFramework
    import CDAlertView
    
    class ColorTableViewController: UITableViewController {
        var chameleonColorNames = ["Maroon (dark)","Maroon (light)",
                                   "Red (dark)","Red (light)",
                                   "Watermelon (dark)","Watermelon (light)",
                                   "Orange (dark)","Orange (light)",
                                   "Yellow (dark)","Yellow (light)",
                                   "Lime (dark)","Lime (light)",
                                   "Green (dark)","Green (light)",
                                   "Mint (dark)","Mint (light)",
                                   "Forest Green(dark)","Forest Green(light)",
                                   "Teal (dark)","Teal (light)",
                                   "Navy Blue(dark)","Navy Blue(light)",
                                   "Blue (dark)","Blue (light)",
                                   "Sky Blue(dark)","Sky Blue(light)",
                                   "Powder Blue(dark)","Powder Blue (light)",
                                   "Plum (dark)","Plum (light)",
                                   "Purple (dark)","Purple (light)",
                                   "Magenta (dark)","Magenta (light)",
                                   "Pink (dark)","Pink (light)",
                                   "Brown (dark)","Brown (light)",
                                   "Coffee (dark)","Coffee (light)",
                                   "Sand (dark)","Sand (light)",
                                   "Black",
                                   "Gray (dark)","Gray (light)",
                                   "White (dark)","White (light)"]
        var chameleonColors = [UIColor.flatMaroonDark,UIColor.flatMaroon,
                               UIColor.flatRedDark,UIColor.flatRed,
                               UIColor.flatWatermelonDark,UIColor.flatWatermelon,
                               UIColor.flatOrangeDark,UIColor.flatOrange,
                               UIColor.flatYellowDark,UIColor.flatYellow,
                               UIColor.flatLimeDark, UIColor.flatLime,
                               UIColor.flatGreenDark,UIColor.flatGreen,
                               UIColor.flatMintDark,UIColor.flatMint,
                               UIColor.flatForestGreenDark,UIColor.flatForestGreen,
                               UIColor.flatTealDark,UIColor.flatTeal,
                               UIColor.flatNavyBlueDark,UIColor.flatNavyBlue,
                               UIColor.flatBlueDark,UIColor.flatBlue,
                               UIColor.flatSkyBlueDark,UIColor.flatSkyBlue,
                               UIColor.flatPowderBlueDark,UIColor.flatPowderBlue,
                               UIColor.flatPlumDark,UIColor.flatPlum,
                               UIColor.flatPurpleDark,UIColor.flatPurple,
                               UIColor.flatMagentaDark,UIColor.flatMagenta,
                               UIColor.flatPinkDark,UIColor.flatPink,
                               UIColor.flatBrownDark,UIColor.flatBrown,
                               UIColor.flatCoffeeDark,UIColor.flatCoffee,
                               UIColor.flatSandDark,UIColor.flatSand,
                               UIColor.flatBlack,
                               UIColor.flatGrayDark,UIColor.flatGray,
                               UIColor.flatWhiteDark,UIColor.flatWhite]
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Uncomment the following line to preserve selection between presentations
            // self.clearsSelectionOnViewWillAppear = false
    
            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        // MARK: - Table view data source
    
        override func numberOfSections(in tableView: UITableView) -> Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of rows
            return chameleonColorNames.count
        }
    
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell", for: indexPath) as! ColorTableViewCell
    
            // Configure the cell...
            cell.colorNameLabel.text = chameleonColorNames[indexPath.row]
            cell.colorView.backgroundColor = chameleonColors[indexPath.row]
    
    
            return cell
        }
        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 43.5
        }
    
        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            print("\(chameleonColorNames [indexPath.row])")
    
            let selectedThemeColor = chameleonColors[indexPath.row]
            let alert = CDAlertView(title: "Theme", message: " Apply \(chameleonColorNames[indexPath.row]) theme?" , type: .notification)
            alert.circleFillColor = selectedThemeColor
    
            alert.hideAnimations = { (center, transform, alpha) in
                transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
                alpha = 0
            }
    
            let doneAction = CDAlertViewAction(title: "Yes", handler: { action in
                self.applyTheme(selectedColor: selectedThemeColor)
                self.navigationController?.popViewController(animated: true)
            })
            let noAction = CDAlertViewAction(title: "No")
            alert.add(action: doneAction)
            alert.add(action: noAction)
            alert.show()
        }
    
        func applyTheme(selectedColor: UIColor) {
            Chameleon.setGlobalThemeUsingPrimaryColor(selectedColor, with: .contrast)
            navigationController?.navigationBar.barTintColor = selectedColor
            let contrastingColor = UIColor(contrastingBlackOrWhiteColorOn:selectedColor, isFlat: true)
            navigationController?.navigationBar.titleTextAttributes = [.foregroundColor : contrastingColor]
            saveThemeColors(thmColor: selectedColor, contrastColor: contrastingColor)
    
        }
        func saveThemeColors(thmColor: UIColor,contrastColor: UIColor)  {
            UserDefaults.standard.set(thmColor, forKey: "themeColor") 
            UserDefaults.standard.set(contrastColor, forKey: "contrastThemeColor")
        }
    }
    extension UserDefaults {
        func set(_ color: UIColor, forKey key: String) {
            set(NSKeyedArchiver.archivedData(withRootObject: color), forKey: key)
        }
        func color(forKey key: String) -> UIColor? {
            guard let data = data(forKey: key) else { return nil }
            return NSKeyedUnarchiver.unarchiveObject(with: data) as? UIColor
        }
    }
    
    //ColorTableViewController.swift

    import UIKit
    import ChameleonFramework
    import QuartzCore
    
    class ViewController: UIViewController {
    
        @IBOutlet weak var segmentedControl: UISegmentedControl!
    
        @IBOutlet weak var tableView: UITableView!
        @IBOutlet weak var textField: UITextField!
    
        @IBOutlet weak var button: UIButton!
    
        var themeColor = UIColor.black {
            didSet{
                if (UserDefaults.standard.value(forKey: "themeColor") != nil) {
                    themeColor = UserDefaults.standard.value(forKey: "themeColor") as! UIColor
                }
                else{
                    themeColor = UIColor.black
                }
            }
        }
        var themeContrastColor = UIColor.white {
            didSet{
                if (UserDefaults.standard.value(forKey: "contrastThemeColor") != nil) {
                    themeContrastColor = UserDefaults.standard.value(forKey: "contrastThemeColor") as! UIColor
                }
                else{
                    themeContrastColor = UIColor.white
                }
            }
        }
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            styleUI()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func styleUI(){
            DispatchQueue.main.async {
                self.segmentedControl.tintColor = self.themeColor
    
                self.tableView.tintColor = self.themeColor
                self.tableView.layer.borderWidth = 1.0
                self.tableView.layer.borderColor = self.themeColor.cgColor
                self.tableView.separatorColor = self.themeColor
    
                self.textField.layer.borderWidth = 1.0
                self.textField.layer.masksToBounds = true
                self.textField.layer.borderColor = self.themeColor.cgColor
                self.textField.tintColor = self.themeColor
    
                self.button.backgroundColor = self.themeColor
                self.button.tintColor = self.themeContrastColor
            }
    
    
        }
    
    
    }
    extension ViewController: UITableViewDelegate, UITableViewDataSource{
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 7
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) 
            cell.textLabel?.text = "Row: \(indexPath.row)"
            return cell
        }
    
    }
    
    import UIKit
    import ChameleonFramework
    import CDAlertView
    
    class ColorTableViewController: UITableViewController {
        var chameleonColorNames = ["Maroon (dark)","Maroon (light)",
                                   "Red (dark)","Red (light)",
                                   "Watermelon (dark)","Watermelon (light)",
                                   "Orange (dark)","Orange (light)",
                                   "Yellow (dark)","Yellow (light)",
                                   "Lime (dark)","Lime (light)",
                                   "Green (dark)","Green (light)",
                                   "Mint (dark)","Mint (light)",
                                   "Forest Green(dark)","Forest Green(light)",
                                   "Teal (dark)","Teal (light)",
                                   "Navy Blue(dark)","Navy Blue(light)",
                                   "Blue (dark)","Blue (light)",
                                   "Sky Blue(dark)","Sky Blue(light)",
                                   "Powder Blue(dark)","Powder Blue (light)",
                                   "Plum (dark)","Plum (light)",
                                   "Purple (dark)","Purple (light)",
                                   "Magenta (dark)","Magenta (light)",
                                   "Pink (dark)","Pink (light)",
                                   "Brown (dark)","Brown (light)",
                                   "Coffee (dark)","Coffee (light)",
                                   "Sand (dark)","Sand (light)",
                                   "Black",
                                   "Gray (dark)","Gray (light)",
                                   "White (dark)","White (light)"]
        var chameleonColors = [UIColor.flatMaroonDark,UIColor.flatMaroon,
                               UIColor.flatRedDark,UIColor.flatRed,
                               UIColor.flatWatermelonDark,UIColor.flatWatermelon,
                               UIColor.flatOrangeDark,UIColor.flatOrange,
                               UIColor.flatYellowDark,UIColor.flatYellow,
                               UIColor.flatLimeDark, UIColor.flatLime,
                               UIColor.flatGreenDark,UIColor.flatGreen,
                               UIColor.flatMintDark,UIColor.flatMint,
                               UIColor.flatForestGreenDark,UIColor.flatForestGreen,
                               UIColor.flatTealDark,UIColor.flatTeal,
                               UIColor.flatNavyBlueDark,UIColor.flatNavyBlue,
                               UIColor.flatBlueDark,UIColor.flatBlue,
                               UIColor.flatSkyBlueDark,UIColor.flatSkyBlue,
                               UIColor.flatPowderBlueDark,UIColor.flatPowderBlue,
                               UIColor.flatPlumDark,UIColor.flatPlum,
                               UIColor.flatPurpleDark,UIColor.flatPurple,
                               UIColor.flatMagentaDark,UIColor.flatMagenta,
                               UIColor.flatPinkDark,UIColor.flatPink,
                               UIColor.flatBrownDark,UIColor.flatBrown,
                               UIColor.flatCoffeeDark,UIColor.flatCoffee,
                               UIColor.flatSandDark,UIColor.flatSand,
                               UIColor.flatBlack,
                               UIColor.flatGrayDark,UIColor.flatGray,
                               UIColor.flatWhiteDark,UIColor.flatWhite]
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Uncomment the following line to preserve selection between presentations
            // self.clearsSelectionOnViewWillAppear = false
    
            // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
            // self.navigationItem.rightBarButtonItem = self.editButtonItem
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        // MARK: - Table view data source
    
        override func numberOfSections(in tableView: UITableView) -> Int {
            // #warning Incomplete implementation, return the number of sections
            return 1
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            // #warning Incomplete implementation, return the number of rows
            return chameleonColorNames.count
        }
    
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "colorCell", for: indexPath) as! ColorTableViewCell
    
            // Configure the cell...
            cell.colorNameLabel.text = chameleonColorNames[indexPath.row]
            cell.colorView.backgroundColor = chameleonColors[indexPath.row]
    
    
            return cell
        }
        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 43.5
        }
    
        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            print("\(chameleonColorNames [indexPath.row])")
    
            let selectedThemeColor = chameleonColors[indexPath.row]
            let alert = CDAlertView(title: "Theme", message: " Apply \(chameleonColorNames[indexPath.row]) theme?" , type: .notification)
            alert.circleFillColor = selectedThemeColor
    
            alert.hideAnimations = { (center, transform, alpha) in
                transform = CGAffineTransform(scaleX: 0.3, y: 0.3)
                alpha = 0
            }
    
            let doneAction = CDAlertViewAction(title: "Yes", handler: { action in
                self.applyTheme(selectedColor: selectedThemeColor)
                self.navigationController?.popViewController(animated: true)
            })
            let noAction = CDAlertViewAction(title: "No")
            alert.add(action: doneAction)
            alert.add(action: noAction)
            alert.show()
        }
    
        func applyTheme(selectedColor: UIColor) {
            Chameleon.setGlobalThemeUsingPrimaryColor(selectedColor, with: .contrast)
            navigationController?.navigationBar.barTintColor = selectedColor
            let contrastingColor = UIColor(contrastingBlackOrWhiteColorOn:selectedColor, isFlat: true)
            navigationController?.navigationBar.titleTextAttributes = [.foregroundColor : contrastingColor]
            saveThemeColors(thmColor: selectedColor, contrastColor: contrastingColor)
    
        }
        func saveThemeColors(thmColor: UIColor,contrastColor: UIColor)  {
            UserDefaults.standard.set(thmColor, forKey: "themeColor") 
            UserDefaults.standard.set(contrastColor, forKey: "contrastThemeColor")
        }
    }
    extension UserDefaults {
        func set(_ color: UIColor, forKey key: String) {
            set(NSKeyedArchiver.archivedData(withRootObject: color), forKey: key)
        }
        func color(forKey key: String) -> UIColor? {
            guard let data = data(forKey: key) else { return nil }
            return NSKeyedUnarchiver.unarchiveObject(with: data) as? UIColor
        }
    }
    

    在将新值存储到变量后,会立即调用didSet。在您的情况下,变量的didSet没有调用。尝试更新您的主题颜色,ViewWillDisplay或ViewDidDisplay块上的SecondTrastColor

    之后,您应该在ViewWillDisplay或ViewDidDisplay中再次调用styleUI()方法

    var themeColor = UIColor.black
    var themeContrastColor = UIColor.white
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        let themeColorData = UserDefaults.standard.value(forKey: "themeColor")
        let contrastColorData = UserDefaults.standard.value(forKey: "contrastThemeColor")
    
        themeColor = (NSKeyedUnarchiver.unarchiveObject(with: themeColorData as! Data) as? UIColor)!
        themeContrastColor = (NSKeyedUnarchiver.unarchiveObject(with: contrastColorData as! Data) as? UIColor)!
    
        styleUI()
    }
    

    或者,您可以使用代理协议通知您的viewController,而不是使用ViewDidDisplay。

    不要使用
    var themeColor=UIColor.black
    ,请尝试使用:

    var themeColor: UIColor? {
        didSet {
    
        }
    }
    

    谢谢你@U.Benlice,感谢你的帮助谢谢你@Daniel Dramond,感谢你的帮助。