2017-02-09 2 views
0

스위치를 사용하여 어두운 모드를 켜면 사용자가 테이블보기 셀 배경색을 검정색으로 변경합니다 (따라서 어두운 모드). 또한 스위치가 켜져있을 때 탐색 표시 줄의 색상을 변경하는 방법에 대해 궁금합니다. 여기 스위치가 켜져있을 때 UITableViewCells의 배경색을 변경하십시오.

Image

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. 
    } 

} 
다른 그림이 표시되어의 배경 색상에 대한 내 주요 스토리 보드

enter image description here

+0

이 코드를 삽입 한 기능은 무엇입니까? – nanothread59

+0

@ nanothread59 셀의 어둡게 모드 스위치가 포함 된 UITableViewController와 연관된 viewdidload 함수의 swift 클래스 파일 –

답변

1

: 아래

는 (전체 코드) 내가 시도 것입니다 UITableViewCells, 당신은 지금처럼 밤 모드가 켜져 있는지를 나타내는 boolean을 가질 수 있습니다.

스위치 값이 변경되면 (off에서 on 또는 viceversa로) tableView.reloadData()으로 전화해야합니다. 이렇게하면 각 셀에 대해 cellForIndexPath 메서드가 다시 호출됩니다. 이 메서드에서는 야간 모드가 켜져 있는지 (부울 값과 함께) 확인하고 그에 따라 셀의 backgroundColor를 적절히 설정해야합니다.

navigationBar의 경우 barTintColor라는 속성을 사용할 수 있습니다. 다음과 같이 사용할 수 있습니다.

UINavigationController?.navigationBar.barTintColor = //your color 

또한 tableView의 데이터 소스 메서드를 구현해야합니다. tableviewController가 이미 datasourcedelegate이므로이를 무시해야합니다. 중요한 세 가지가 있습니다.

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

두 가지 대답을 받아 들일 수 있다면, Frederico는 이것을 달성하는데 나를 도왔습니다. 그냥 알려주고 싶었어. 감사! –

관련 문제