我想用3个选项实施iOS暗模式测试:亮,暗和系统。前两个将强制应用处于该特定状态,而不遵循系统设置。
现在,我想在多个视图而不是单个视图中实现此设置,我在第二个视图中创建了这些选项。但是我现在面临两个问题:
下面是我的第二视图代码
import UIKit
class DarkModeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var darkModeTable: UITableView!
var statusBarStyle = UIStatusBarStyle.default { didSet { setNeedsStatusBarAppearanceUpdate() } }
override var preferredStatusBarStyle: UIStatusBarStyle { statusBarStyle }
let mode = ["Light", "Dark", "System"]
var currentStatus = ""
override func viewDidLoad() {
super.viewDidLoad()
switch currentStatus {
case "Light":
overrideUserInterfaceStyle = .light
statusBarStyle = .darkContent
break
case "Dark":
overrideUserInterfaceStyle = .dark
statusBarStyle = .lightContent
break
case "System":
overrideUserInterfaceStyle = .unspecified
statusBarStyle = .default
break
default:
break
}
darkModeTable.dataSource = self
darkModeTable.delegate = self
}
// Table View Data Source method
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// Table View Data Source method
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mode.count
}
// Table View Data Source method
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath)
-> UITableViewCell {
var cell: UITableViewCell!
//take a cell from the queue of reusable cells
cell = tableView.dequeueReusableCell(
withIdentifier: "tableCell")
//if there are no reusable cells
if cell == nil {
// create a new cell
cell = UITableViewCell(
style: UITableViewCell.CellStyle.default,
reuseIdentifier: "tableCell")
}
// set the textLabel for the cell
cell!.textLabel!.text = mode[indexPath.row]
if (overrideUserInterfaceStyle == .light){
if (mode[indexPath.row] == "Light"){
cell.accessoryType = .checkmark
currentStatus = "Light"
}
else{
cell.accessoryType = .none
}
}
else if (overrideUserInterfaceStyle == .dark){
if (mode[indexPath.row] == "Dark"){
cell.accessoryType = .checkmark
currentStatus = "Dark"
}
else{
cell.accessoryType = .none
}
}
else if (overrideUserInterfaceStyle == .unspecified){
if (mode[indexPath.row] == "System"){
cell.accessoryType = .checkmark
currentStatus = "System"
}
else{
cell.accessoryType = .none
}
}
// return the Table View Cell
return cell!
}
// Table View Delegate method
func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) {
//clear previous stored chacked indexPath
for row in 0..<tableView.numberOfRows(inSection: indexPath.section) {
if let cell = tableView.cellForRow(at: IndexPath(row: row, section: indexPath.section)) {
cell.accessoryType = row == indexPath.row ? .checkmark : .none
}
}
if(indexPath.row == 0)
{
overrideUserInterfaceStyle = .light
statusBarStyle = .darkContent
}
else if(indexPath.row == 1)
{
overrideUserInterfaceStyle = .dark
statusBarStyle = .lightContent
}
else if(indexPath.row == 2)
{
overrideUserInterfaceStyle = .unspecified
statusBarStyle = .default
}
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
}