gpt4 book ai didi

ios - 为什么选择按钮后按钮背景变为蓝色?

转载 作者:行者123 更新时间:2023-12-01 18:03:31 24 4
gpt4 key购买 nike

我有三个按钮,当它们被选中时,它们会将背景颜色更改为红色-但是还会弹出一个蓝色背景,我不知道为什么(抱歉,如果原因很明显,我只做了3个星期的“编程”)。我在代码中找不到任何能解释它的内容我有三个按钮,当它们被选中时,它们会将背景颜色更改为红色-但蓝色背景也弹出了,我也不知道为什么(抱歉,原因很明显,香港专业教育学院仅进行了3周的“编程”,我在代码中找不到任何可以解释的内容

[enter image description here][1]import UIKit

class ViewController: UIViewController {

var counterEN : Int = 0
var count = Array(1...150).filter { $0 % 1 != 0}.count
var pizzaCount = 0


@IBOutlet weak var pizzaCounter: UILabel!
@IBOutlet weak var textField: UITextField!
@IBAction func test(_ sender: UIButton) {
self.textField.text = sender.currentTitle
}

var lastY: CGFloat = 100

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func buttonClicked(_ sender: UIButton) {
pizzaCount = pizzaCount + 1
pizzaCounter.text = ("Antal pizza: ") + "\(pizzaCount)"
let contentView = UIView()
addViewsTo(contentView, title: sender.currentTitle)
contentView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(contentView)


// Add size constraints to the content view (260, 30)
NSLayoutConstraint(item: contentView, attribute: .width, relatedBy: .equal,
toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 850.0).isActive = true
NSLayoutConstraint(item: contentView, attribute: .height, relatedBy: .equal,
toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: ` 30.0).isActive = true
// Add position constraints to the content view (horizontal center, 100 from the top)
NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: lastY).isActive = true
NSLayoutConstraint(item: contentView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0).isActive = true

// Update last Y position to have the gaps between views to be 10
lastY += 50
}

// Add label and button to the content view
func addViewsTo(_ contentView: UIView, title: String?) {
// Add a label with size of (100, 30)
let label = UILabel()
label.text = title
//label.text = ("1 x 17 ")

label.frame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 30.0)
contentView.addSubview(label)



let DressingButton = UIButton(type: .system)

DressingButton.frame = CGRect(x: 210, y: 0, width: 40, height: 30)
DressingButton.setTitle("D", for: .normal)
DressingButton.setTitleColor(.black, for: .normal)

DressingButton.setTitleColor(.red, for: .selected)
DressingButton.sendActions(for: .touchUpInside)
contentView.addSubview(DressingButton)

DressingButton.addTarget(self, action: #selector(dressingAction(_:)), for .touchUpInside)


let Chili = UIButton(type: .system)

Chili.frame = CGRect(x: 160, y: 0, width: 40, height: 30)
Chili.setTitle("C", for: .normal)
Chili.setTitleColor(.black, for: .normal)
Chili.backgroundColor = UIColor.white
Chili.setTitleColor(.systemRed, for: .selected)
Chili.sendActions(for: .touchUpInside)
contentView.addSubview(Chili)


Chili.addTarget(self, action: #selector(dressingAction(_:)), for: .touchUpInside)

let Hvidløg = UIButton(type: .system)

Hvidløg.frame = CGRect(x: 110, y: 0, width: 40, height: 30)
Hvidløg.setTitle("H", for: .normal)
Hvidløg.setTitleColor(.black, for: .normal)
Hvidløg.setTitleColor(.red, for: .selected)
Hvidløg.backgroundColor = UIColor.white

Hvidløg.sendActions(for: .touchUpInside)


contentView.addSubview(Hvidløg)

Hvidløg.addTarget(self, action: #selector(dressingAction(_:)), for: .touchUpInside)


let button2 = UIButton(type: .system)

button2.frame = CGRect(x: -40, y: 20, width: 150, height: 30)
button2.setTitle("Alm", for: .normal)
button2.setTitle("Ful", for: .selected)
button2.setTitle("Glu", for: .selected)
// Set button action
button2.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)

contentView.addSubview(button2)
self.view = view
}
@objc func dressingAction(_ sender:UIButton){

sender.isSelected.toggle()
print("knap")
sender.backgroundColor = UIColor.red
}

@objc func buttonAction(_ sender:UIButton!)
{

sender.isSelected = !sender.isSelected
sender.setTitle("Alm", for: .normal)

if sender.isSelected{
sender.setTitle("Fuld", for: .selected)}
else {
sender.setTitle("Glu", for: .selected)
sender.isSelected = !sender.isSelected
// sender.tintColor = .clear

//// if sender.isSelected{
// sender.setTitleColor(.green, for: .selected)
// }
// else{
// sender.setTitleColor(.blue, for: .selected)
//
func buttonAction(_ button3:UIButton!) {

print("Button tapped"); counterEN += 1; count += 1; print (counterEN); print (count)

}

}
}
}

最佳答案

请将您的按钮类型更改为.custom,此问题将得到解决。

let dressingButton = UIButton(type: .custom)

关于ios - 为什么选择按钮后按钮背景变为蓝色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62379150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com