暗模式iOS实施期间在多个视图上出现暗模式和问题

时间:2020-08-06 12:38:23

标签: ios swift ios13 ios-darkmode

我想用3个选项实施iOS暗模式测试:亮,暗和系统。前两个将强制应用处于该特定状态,而不遵循系统设置。

现在,我想在多个视图而不是单个视图中实现此设置,我在第二个视图中创建了这些选项。但是我现在面临两个问题:

  1. “亮”和“暗”仅影响第二个视图,而不影响所有视图。
  2. 我在情节提要中以模态形式使用了礼物。当我在第二个视图中将选项从“系统”更改为“暗”时,它将更改为暗模式。但是,如果我关闭第二个视图并单击自定义按钮再次打开,它会显示回“系统”状态。

下面是我的第二视图代码

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
    }

}

0 个答案:

没有答案