![](/img/trans.png)
[英]Changing UIAlertController textfield's background color doesn't work
[英]Back button doesn't work when changing background color
我有一个显示水果列表的应用程序。 当你点击其中一个时,会弹出一个视图,其中包含该水果的所有信息和营养价值。 如果我点击的水果含糖量超过 10,背景应该在红色和白色之间重复。 我的问题是我有一个后退按钮 (Tilbake),它适用于每个视图,除了具有超过 10 糖的动画背景颜色的视图。 向下滑动视图的功能也停止工作。 它似乎是屏幕顶部的某种层,阻止我实际触摸屏幕。 我该如何解决这个问题?
import UIKit
class FruitDetailVC: UIViewController{
var fruit: Fruit?
var safeArea: UILayoutGuide!
let nameLabel = UILabel()
let genusLabel = UILabel()
let familyLabel = UILabel()
let orderLabel = UILabel()
let idLabel = UILabel()
let carbLabel = UILabel()
let proteinLabel = UILabel()
let fatLabel = UILabel()
let caloriesLabel = UILabel()
let sugarLabel = UILabel()
let dividerLabel = UILabel()
let sugarWarningLabel = UILabel()
let dismissButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
setupdata()
setupLabel()
setupDismissButton()
safeArea = view.layoutMarginsGuide
view.backgroundColor = .white
if (fruit?.nutritions.sugar)! > 10{
sugarWarningLabel.text = "This contains over 10 of sugar!!!"
animatedBackground()
}
}
func animatedBackground(){
UIView.animate(withDuration: 1.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
self.view.backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
}, completion:{
(completed : Bool) -> Void in
UIView.animate(withDuration: 1.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
self.view.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)
}, completion:nil)
})
}
func setupLabel(){
view.addSubview(nameLabel)
view.addSubview(genusLabel)
view.addSubview(familyLabel)
view.addSubview(orderLabel)
view.addSubview(idLabel)
view.addSubview(carbLabel)
view.addSubview(proteinLabel)
view.addSubview(fatLabel)
view.addSubview(caloriesLabel)
view.addSubview(sugarLabel)
view.addSubview(dividerLabel)
view.addSubview(sugarWarningLabel)
//General infromation about selected fruit
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
nameLabel.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
nameLabel.font = UIFont(name: "Verdana-Bold", size: 16)
genusLabel.translatesAutoresizingMaskIntoConstraints = false
genusLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
genusLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor).isActive = true
genusLabel.font = UIFont(name: "Verdana", size: 14)
familyLabel.translatesAutoresizingMaskIntoConstraints = false
familyLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
familyLabel.topAnchor.constraint(equalTo: genusLabel.bottomAnchor).isActive = true
familyLabel.font = UIFont(name: "Verdana", size: 14)
orderLabel.translatesAutoresizingMaskIntoConstraints = false
orderLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
orderLabel.topAnchor.constraint(equalTo: familyLabel.bottomAnchor).isActive = true
orderLabel.font = UIFont(name: "Verdana", size: 14)
idLabel.translatesAutoresizingMaskIntoConstraints = false
idLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
idLabel.topAnchor.constraint(equalTo: orderLabel.bottomAnchor).isActive = true
idLabel.font = UIFont(name: "Verdana", size: 14)
//Nutrition information divider
dividerLabel.translatesAutoresizingMaskIntoConstraints = false
dividerLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
dividerLabel.topAnchor.constraint(equalTo: idLabel.bottomAnchor).isActive = true
dividerLabel.font = UIFont(name: "Verdana-Bold", size: 16)
//Nutrition information
carbLabel.translatesAutoresizingMaskIntoConstraints = false
carbLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
carbLabel.topAnchor.constraint(equalTo: dividerLabel.bottomAnchor).isActive = true
carbLabel.font = UIFont(name: "Verdana", size: 14)
proteinLabel.translatesAutoresizingMaskIntoConstraints = false
proteinLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
proteinLabel.topAnchor.constraint(equalTo: carbLabel.bottomAnchor).isActive = true
proteinLabel.font = UIFont(name: "Verdana", size: 14)
fatLabel.translatesAutoresizingMaskIntoConstraints = false
fatLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
fatLabel.topAnchor.constraint(equalTo: proteinLabel.bottomAnchor).isActive = true
fatLabel.font = UIFont(name: "Verdana", size: 14)
sugarLabel.translatesAutoresizingMaskIntoConstraints = false
sugarLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
sugarLabel.topAnchor.constraint(equalTo: fatLabel.bottomAnchor).isActive = true
sugarLabel.font = UIFont(name: "Verdana", size: 14)
sugarWarningLabel.translatesAutoresizingMaskIntoConstraints = false
sugarWarningLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
sugarWarningLabel.topAnchor.constraint(equalTo: sugarLabel.bottomAnchor).isActive = true
sugarWarningLabel.font = UIFont(name: "Verdana-Bold", size: 14)
}
func setupdata(){
if let fruit = fruit{
nameLabel.text = fruit.name
genusLabel.text = "Genus : \(fruit.genus)"
familyLabel.text = "Family : \(fruit.family)"
orderLabel.text = "Order : \(fruit.order)"
dividerLabel.text = "Nutritional value"
let id = fruit.id
let idString = String(id)
idLabel.text = "ID : \(idString)"
let carbs = fruit.nutritions.carbohydrates
let carbString = String(carbs)
carbLabel.text = "Carbohydrates : \(carbString)"
let protein = fruit.nutritions.protein
let proteinString = String(protein)
proteinLabel.text = "Proteins : \(proteinString)"
let fat = fruit.nutritions.fat
let fatString = String(fat)
fatLabel.text = "Fat : \(fatString)"
let calories = fruit.nutritions.calories
let calorieString = String(calories)
caloriesLabel.text = "Carbohydrates : \(calorieString)"
let sugar = fruit.nutritions.sugar
let sugarString = String(sugar)
sugarLabel.text = "Sugar : \(sugarString)"
}
}
func setupDismissButton(){
view.addSubview(dismissButton)
dismissButton.translatesAutoresizingMaskIntoConstraints = false
dismissButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
dismissButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
dismissButton.setTitle("Tilbake",for: .normal)
dismissButton.setTitleColor(UIColor.black, for: .normal)
dismissButton.addTarget(self, action: #selector(dismissAction), for: .touchUpInside)
}
@objc func dismissAction(){
self.dismiss(animated: true)
}
}
您的 animation 的“表示层”可能覆盖了您的关闭按钮并防止它被点击。 尝试向您的dismissAction()
添加打印语句,并查看当您点击具有动画背景的屏幕上的按钮时是否调用它。
这是一个猜测,但一个相当不错的猜测。
要解决此问题,请尝试在所有其他视图(按钮、标签等)下向您的视图的内容视图添加一个视图。让我们将该新视图称为您的 colorView。 更改您的 animation 以对 colorView 的背景颜色进行动画处理,而不是对视图控制器的内容视图的颜色进行动画处理。 我不确定这会解决它,但值得一试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.