我想让用户在打开暗模式(使用开关)时将表格视图单元格背景颜色更改为黑色(因此是暗模式)。我也想知道如何在开关打开时更改导航栏的颜色。当开关打开时更改UITableViewCells的背景颜色 - Swift
下面是我曾尝试(全码):
import Foundation
import UIKit
class SideMenuController8: UITableViewController{
@IBOutlet var TableViewColor: UITableView!
@IBOutlet weak var OpenSettings: UIBarButtonItem!
@IBOutlet weak var mSwitch: UISwitch!
@IBOutlet weak var dSwitch: UISwitch!
override func viewDidLoad() {
self.navigationController?.navigationBar.topItem!.title = "Settings"
if revealViewController() != nil {
OpenSettings.target = revealViewController()
OpenSettings.action = #selector(SWRevealViewController.revealToggle(_:))
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
// mSwitch.layer.borderWidth = 1
// mSwitch.layer.borderColor = UIColor.white.cgColor
let onColor = UIColor(red: CGFloat(0.0), green: CGFloat(122.0/255.0), blue: CGFloat(1.0), alpha: CGFloat(1.0))
let offColor = UIColor.white
//Notifications On Switch
mSwitch.isOn = false
/*For on state*/
mSwitch.onTintColor = onColor
/*For off state*/
mSwitch.tintColor = offColor
mSwitch.layer.cornerRadius = 16
mSwitch.backgroundColor = offColor
//Dark Mode Switch
dSwitch.isOn = false
/*For on state*/
dSwitch.onTintColor = onColor
/*For off state*/
dSwitch.tintColor = offColor
dSwitch.layer.cornerRadius = 16
dSwitch.backgroundColor = offColor
if (dSwitch.isOn == true){
TableViewColor.reloadData()
print("Dark Mode Switch is on")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这里是另一个PIC显示我的主要情节串连图板
你把这段代码放在什么函数中? – nanothread59
@ nanothread59在与包含单元格中的暗模式开关的UITableViewController相关联的viewdidload函数中的swift类文件中 –