UITableView 和 UITextField borderColor 问题
UITableView and UITextField borderColor issue
我是 Swift 的新手,边做边学。无法查明我的错误。
是我编码错误还是实现错误。
我正在尝试在 Chameleon 框架的帮助下实现主题功能。
我已经能够更改一些 UI 元素的颜色,例如 UISegmentedControl
和 UIButton
,但我无法更改 borderColor
的 UITextField
和 borderColor
以及 UITableView
的 separatorColor。 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
}
}
//颜色表ViewController.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
}
}
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。
而不是使用 var themeColor = UIColor.black
,尝试只使用:
var themeColor: UIColor? {
didSet {
}
}
我是 Swift 的新手,边做边学。无法查明我的错误。 是我编码错误还是实现错误。
我正在尝试在 Chameleon 框架的帮助下实现主题功能。
我已经能够更改一些 UI 元素的颜色,例如 UISegmentedControl
和 UIButton
,但我无法更改 borderColor
的 UITextField
和 borderColor
以及 UITableView
的 separatorColor。 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
}
}
//颜色表ViewController.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
}
}
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。
而不是使用 var themeColor = UIColor.black
,尝试只使用:
var themeColor: UIColor? {
didSet {
}
}