Я новичок в Swift, учусь больше на деле.Невозможно определить мою ошибку.Неважно, кодирую я неправильно или реализую неправильно.
Я пытаюсь реализовать функцию создания тем с помощью Chameleon framework.Мне удалось изменить цвет некоторых элементов пользовательского интерфейса, таких как UISegmentedControl
и UIButton
, но я не смог изменить borderColor
для UITextField
и borderColor
, а такжеРазделительЦвет от UITableView
.Изменения цвета для UITextField
и UITableView
выполняются на слоях соответствующего компонента.Может ли слой быть моей проблемой?
Просмотр:
- 1-й экран:
ViewController
- 2-й экран:
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
}
}