【发布时间】:2017-02-09 23:43:01
【问题描述】:
我试图让用户在打开暗模式时(使用开关)将表格视图单元格的背景颜色更改为黑色(因此是暗模式)。我也想知道如何在开关打开时更改导航栏的颜色。
以下是我尝试过的(完整代码):
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.
}
}
这是另一张展示我的主要故事板的图片
【问题讨论】:
-
你把这段代码放在什么函数里了?
-
@nanothread59 在与 UITableViewController 关联的 viewdidload 函数中的 swift 类文件中,其中包含单元格中的暗模式开关
标签: swift xcode uitableview