UITableView 和 UITextField 边框颜色问题

Posted

技术标签:

【中文标题】UITableView 和 UITextField 边框颜色问题【英文标题】:UITableView and UITextField borderColor issue 【发布时间】:2018-05-28 13:15:50 【问题描述】:

我是 Swift 新手,边做边学。无法查明我的错误。 无论是我编码错误还是实现错误。

我正在尝试在Chameleon 框架的帮助下实现主题功能。 我已经能够更改一些 UI 元素的颜色,例如 UISegmentedControlUIButton,但我无法更改 UITextFieldborderColorborderColor 以及UITableView 的分隔符颜色。 UITextFieldUITableView 的颜色更改在各自组件的图层上完成。 层可能是我的问题吗?

预览:

    第一屏: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
    


// ColorTableViewController.swift

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
    

【问题讨论】:

【参考方案1】:

didSet 在新值存储到变量后立即调用。在您的情况下,您的变量的 didSet 没有调用。尝试更新 viewWillAppear 或 viewDidAppear 块上的主题颜色和 themeContrastColor。

之后,您应该在 viewWillAppear 或 viewDidAppear 中再次调用您的 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 而不是使用 viewDidAppear。

【讨论】:

谢谢@U。 Benlice,感谢您的帮助??。您的解决方案有效,尽管我不得不对其进行一些调整。为遇到类似问题的其他人更新解决方案。【参考方案2】:

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

var themeColor: UIColor? 
    didSet 

    

【讨论】:

谢谢@Daniel Dramond,感谢您的帮助。

以上是关于UITableView 和 UITextField 边框颜色问题的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 和 UITextField 边框颜色问题

键盘出现时向上移动 UITextField 和 UITableView

从 UITableView 获取 UITextField 文本

UITableViewCell 与 UITextField 失去选择 UITableView 行的能力?

处理 UItableView 中 UItextfield 的委托

如何在 UITableView 中激活下一个和上一个 UITextField?