我将tableView背景色设置为与tableViewCell背景色相同,但是颜色看起来不同。设置为自定义颜色时,颜色看起来相同,但是当未设置自定义颜色时,颜色则不同。 isColorSet在启动时为0,因此两者都应设置为UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)
。如何获得相同的颜色?
设置tableView背景颜色:
override func viewWillAppear(_ animated: Bool) {
//Set background color
if userDefaults.integer(forKey: "isColorSet") == 0 {
navigationController?.navigationBar.barTintColor = UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)
tableView.backgroundColor = UIColor(red: 74/255, green: 187/255, blue: 224/255, alpha: 1.0)
} else if userDefaults.integer(forKey: "isColorSet") == 1 {
let red = CGFloat(userDefaults.float(forKey: "red"))
let green = CGFloat(userDefaults.float(forKey: "green"))
let blue = CGFloat(userDefaults.float(forKey: "blue"))
navigationController?.navigationBar.barTintColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
tableView.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
}
}
设置tableViewCell背景色:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ideaCell") as! IdeaTableViewCell
cell.ideaTitleLabel.text = ideas[indexPath.section][indexPath.row].title
cell.ideaDescLabel.text = ideas[indexPath.section][indexPath.row].desc
//Set date
let date = userDefaults.object(forKey: "date.\(indexPath.section).\(indexPath.row)") as! Date
let month = Calendar.current.component(.month, from: date)
let day = Calendar.current.component(.day, from: date)
let year = Calendar.current.component(.year, from: date)
if userDefaults.string(forKey: "dateStyle") == "MDY" {
cell.ideaDateLabel.text = "\(month)/\(day)/\(year)"
} else if userDefaults.string(forKey: "dateStyle") == "DMY" {
cell.ideaDateLabel.text = "\(day)/\(month)/\(year)"
} else if userDefaults.string(forKey: "dateStyle") == "YMD" {
cell.ideaDateLabel.text = "\(year)/\(month)/\(day)"
}
//Set color
if userDefaults.integer(forKey: "isColorSet") == 0 {
cell.backgroundColor = UIColor(red: 74/255, green: 187/225, blue: 224/225, alpha: 1.0)
} else if userDefaults.integer(forKey: "isColorSet") == 1 {
let red = CGFloat(userDefaults.float(forKey: "red"))
let green = CGFloat(userDefaults.float(forKey: "green"))
let blue = CGFloat(userDefaults.float(forKey: "blue"))
cell.backgroundColor = UIColor(red: red/255, green: green/255, blue: blue/255, alpha: 1.0)
}
return cell
设置UserDefaults的rgb颜色:
func HSBColorColorPickerTouched(sender: HSBColorPicker, color: UIColor, point: CGPoint, state: UIGestureRecognizer.State) {
setButton.backgroundColor = color
red = color.rgb()?.red
green = color.rgb()?.green
blue = color.rgb()?.blue
redLabel.text = "Red: \(red!)"
redSlider.value = Float(red)
greenLabel.text = "Green: \(green!)"
greenSlider.value = Float(green)
blueLabel.text = "Blue: \(blue!)"
blueSlider.value = Float(blue)
}
@IBAction func sliderValueChanged(_ sender: Any) {
red = redSlider.value
green = greenSlider.value
blue = blueSlider.value
if red != nil {
redLabel.text = "Red: \(red!)"
}
if green != nil {
greenLabel.text = "Green: \(green!)"
}
if blue != nil {
blueLabel.text = "Blue: \(blue!)"
}
setButton.backgroundColor = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1.0)
}
@IBAction func setColor(_ sender: Any) {
userDefaults.removeObject(forKey: "red")
userDefaults.removeObject(forKey: "green")
userDefaults.removeObject(forKey: "blue")
userDefaults.set(red, forKey: "red")
userDefaults.set(green, forKey: "green")
userDefaults.set(blue, forKey: "blue")
if userDefaults.float(forKey: "isColorSet") == 0 {
userDefaults.set(1, forKey: "isColorSet")
}
_ = navigationController?.popToRootViewController(animated: true)
}
Screenshot with isColorSet = 0
答案 0 :(得分:0)
问题是以下行中的一个简单错字:
cell.backgroundColor = UIColor(red: 74/255, green: 187/225, blue: 224/225, alpha: 1.0)
绿色和蓝色值的分母应该为255。