我试图做到这一点,以便用户在打开暗模式(使用开关)时将表格视图单元格的背景色更改为黑色(因此为暗模式)。我也想知道如何在打开开关的同时更改导航栏的颜色。
以下是我尝试过的(完整代码):
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.
}
}
这是另一个图片,显示了我的主要情节提要
对于UITableViewCells的背景色,可以像现在一样使用布尔值指示夜间模式是否打开。
When there is a change in the switch value (from off to on or viceversa), you should call tableView.reloadData()
. This way, the method cellForIndexPath
will be called again for each one of the cell. In this method, you should check if the night mode is on (with the boolean) and therefore set the backgroundColor of the cell accordingly.
For the navigationBar, you could use the property called barTintColor. You can use it the following way
UINavigationController?.navigationBar.barTintColor = //your color
Also, remember that you should implement the datasource methods of the tableView. Since your tableviewController is already the datasource
and delegate
, you just have to override them. There are 3 important ones.
//Number of sections
override func numberOfSections(in tableView: UITableView) -> Int
//Number of rows in a section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
//In this one you setup or return a tableviewcell for the specific
//IndexPath. Here you should create a UITableViewCell and set its background
//color accordingly to the value of the switch
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
此方法是UITableViewController的基本方法。这可能超出了问题的范围,您可以在几个来源中查看更多信息。这个示例说明了更多内容https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句