I posted this earlier today but upon reading it, I realized it made no sense, probably the result of working on this simple issue for hours nonstop! Anyways, please go through this code and tell me why the buttons's array which containts the letterButtons, has a count of 54 when it should have a count of 27. Thank you!
"""
import UIKit
class ViewController: UIViewController {
var buttons = [UIButton]()
var scoreLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
loadView()
// Do any additional setup after loading the view.
}
override func loadView() {
view = UIView()
view.backgroundColor = .white
scoreLabel = UILabel()
scoreLabel.translatesAutoresizingMaskIntoConstraints = false
scoreLabel.text = "words"
view.addSubview(scoreLabel)
let buttonsView = UIView()
buttonsView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(buttonsView)
NSLayoutConstraint.activate([
scoreLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 0),
scoreLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: 0),
buttonsView.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor, constant: 20),
buttonsView.widthAnchor.constraint(equalToConstant: 750),
buttonsView.heightAnchor.constraint(equalToConstant: 320),
buttonsView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
buttonsView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -40)
])
let width=76
let height=55
for row in 0..<3 {
for col in 0..<9 {
let letterButton = UIButton(type: .system)
letterButton.titleLabel?.font = UIFont.systemFont(ofSize: 33)
letterButton.setTitle("W", for: .normal)
let frame = CGRect(x: col * width, y: row * height, width: width, height: height)
letterButton.frame = frame
buttons.append(letterButton)
buttonsView.addSubview(letterButton)
print("the count of the buttons is \(buttons.count)")
}
}
}
}
"""