我想讓用戶在打開暗模式(使用開關)時將表格視圖單元格背景顏色更改爲黑色(因此是暗模式)。我也想知道如何在開關打開時更改導航欄的顏色。當開關打開時更改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類文件中 –