You can set the image color in a few different ways...
Two options:
if let img = UIImage(systemName: "lock.icloud")?.withTintColor(.systemRed, renderingMode: .alwaysOriginal) {
imgView.image = img
}
or:
if let img = UIImage(systemName: "lock.icloud") {
imgView.image = img
}
imgView.tintColor = .systemRed
Here is some example code... using the first method to create a Red image, and the second method to create a Green image:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imgViewA = UIImageView()
let imgViewB = UIImageView()
if let img = UIImage(systemName: "lock.icloud")?.withTintColor(.systemRed, renderingMode: .alwaysOriginal) {
imgViewA.image = img
}
if let img = UIImage(systemName: "lock.icloud") {
imgViewB.image = img
}
imgViewB.tintColor = .systemGreen
imgViewA.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(imgViewA)
imgViewB.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(imgViewB)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
imgViewA.topAnchor.constraint(equalTo: g.topAnchor, constant: 80.0),
imgViewA.centerXAnchor.constraint(equalTo: g.centerXAnchor, constant: 0.0),
imgViewA.heightAnchor.constraint(equalToConstant: 80.0),
imgViewA.widthAnchor.constraint(equalTo: imgViewA.heightAnchor),
imgViewB.topAnchor.constraint(equalTo: imgViewA.bottomAnchor, constant: 20.0),
imgViewB.centerXAnchor.constraint(equalTo: g.centerXAnchor, constant: 0.0),
imgViewB.heightAnchor.constraint(equalToConstant: 80.0),
imgViewB.widthAnchor.constraint(equalTo: imgViewB.heightAnchor),
])
}
}
Result:
