如何更改图像 UIButton 的大小和颜色?
How to change size and color of image UIButton?
我有自己创建的 UIButton,就像这样
let btn = UIButton()
view.addSubview(btn)
btn.setImage(UIImage(systemName: "star"), for: .normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.frame = CGRect(x: 30, y: 30, width: 150, height: 150)
btn.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 35).isActive = true
btn.rightAnchor.constraint(equalTo: btnDelete.leftAnchor, constant: 0).isActive = true
btn.heightAnchor.constraint(equalToConstant: 44).isActive = true
btn.widthAnchor.constraint(equalToConstant: 44).isActive = true
问题是我看到了星形图标按钮,但我找不到办法让它变大并改变星形本身的颜色,目前它是蓝色的,但我需要它是白色的。
为了让它更大,请使用下面的代码来填充您在 btn.frame
中给出的宽度和高度:
btn.contentHorizontalAlignment = .fill
btn.contentVerticalAlignment = .fill
要改变图标的颜色,请使用:
btn.tintColor = .white
我有自己创建的 UIButton,就像这样
let btn = UIButton()
view.addSubview(btn)
btn.setImage(UIImage(systemName: "star"), for: .normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.frame = CGRect(x: 30, y: 30, width: 150, height: 150)
btn.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 35).isActive = true
btn.rightAnchor.constraint(equalTo: btnDelete.leftAnchor, constant: 0).isActive = true
btn.heightAnchor.constraint(equalToConstant: 44).isActive = true
btn.widthAnchor.constraint(equalToConstant: 44).isActive = true
问题是我看到了星形图标按钮,但我找不到办法让它变大并改变星形本身的颜色,目前它是蓝色的,但我需要它是白色的。
为了让它更大,请使用下面的代码来填充您在 btn.frame
中给出的宽度和高度:
btn.contentHorizontalAlignment = .fill
btn.contentVerticalAlignment = .fill
要改变图标的颜色,请使用:
btn.tintColor = .white