2017-02-09 92 views
0

我想让用户在打开暗模式(使用开关)时将表格视图单元格背景颜色更改为黑色(因此是暗模式)。我也想知道如何在开关打开时更改导航栏的颜色。当开关打开时更改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. 
    } 

} 

Image

这里是另一个PIC显示我的主要情节串连图板

enter image description here

+0

你把这段代码放在什么函数中? – nanothread59

+0

@ nanothread59在与包含单元格中的暗模式开关的UITableViewController相关联的viewdidload函数中的swift类文件中 –

回答

1

有关的背景颜色UITableViewCells,你可以有一个布尔值,表示夜间模式是否开启,就像你现在一样。

当开关值发生变化时(从关闭到开启或反之亦然),您应该拨打tableView.reloadData()。这样,将再次为每个单元调用方法cellForIndexPath。在这种方法中,您应该检查夜间模式是否开启(使用布尔值),并因此相应地设置单元格的backgroundColor。

对于navigationBar,您可以使用名为barTintColor的属性。你可以通过以下方式使用它

UINavigationController?.navigationBar.barTintColor = //your color 

另外,请记住你应该实现tableView的数据源方法。由于你的tableviewController已经是datasourcedelegate,你只需要重写它们。有3个重要的。

//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/

+0

由于某种原因,背景颜色不变。我刚刚上传了显示此问题的图片。有什么建议? –

+0

我在你的代码中看不到它,但你是否实现了tableview的数据源方法,不是吗?你能上传这些方法的代码吗?特别是cellForRowAt indexPath之一:IndexPath –

+0

如何实现tableview的数据源方法? –

1

要更改单元格背景颜色使用此:

cell.contentView.backgroundColor = //Your Color 

或者

cell.backgroundColor = //Your Color 

相反的tableview的,你应该使用电池来改变颜色。

+0

要更改导航栏颜色,请点击此链接:http://stackoverflow.com/questions/39931463/swift-ios-change-the-color-of-a-navigation-bar –

+0

@Rajat Khare请查看本教程,您将会在iOS中获得有关tableview实现的完整思路:http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/ –

+0

如果我能接受两个答案,我会的,因为你们弗雷德里科帮助我实现了这一目标。只是想让你知道。谢谢! –