2017-02-09 220 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

如果我能接受兩個答案,我會的,因爲你們弗雷德里科幫助我實現了這一目標。只是想讓你知道。謝謝! –