I am currently working on the UI and for some reason, the scoreLabel space extends down vertically. I will add screenshots for better clarification. I have tried setting the ContentHuggingPriority to 1, but it doesn't change anything. How can I rectify this situation? https://imgur.com/a/IoU8hHT
import UIKit
class ViewController: UIViewController {
var wordToGuess: UITextField!
var scoreLabel: UILabel!
var listOfWords = [String]()
var letterBtns = [UIButton]()
var pressedBtns = [UIButton]()
override func loadView() {
view = UIView()
view.backgroundColor = .white
scoreLabel = UILabel()
scoreLabel.translatesAutoresizingMaskIntoConstraints = false
scoreLabel.textAlignment = .right
scoreLabel.text = "Score: 0"
scoreLabel.setContentHuggingPriority(UILayoutPriority(1), for: .vertical)
view.addSubview(scoreLabel)
wordToGuess = UITextField()
wordToGuess.translatesAutoresizingMaskIntoConstraints = false
wordToGuess.textAlignment = .center
wordToGuess.font = UIFont.systemFont(ofSize: 44)
wordToGuess.isUserInteractionEnabled = false
wordToGuess.placeholder = "------"
view.addSubview(wordToGuess)
let newGameBtn = UIButton(type: .system)
newGameBtn.translatesAutoresizingMaskIntoConstraints = false
newGameBtn.setTitle("New Game", for: .normal)
// newGameBtn.addTarget(self, action: #selector(newGameTapped), for: .touchUpInside)
view.addSubview(newGameBtn)
let buttonView = UIView()
buttonView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(buttonView)
NSLayoutConstraint.activate([
scoreLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
scoreLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
wordToGuess.centerXAnchor.constraint(equalTo: view.centerXAnchor),
wordToGuess.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5),
wordToGuess.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor, constant: 100),
newGameBtn.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
newGameBtn.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
buttonView.widthAnchor.constraint(equalToConstant: 750),
buttonView.heightAnchor.constraint(equalToConstant: 320),
buttonView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
buttonView.topAnchor.constraint(equalTo: wordToGuess.bottomAnchor, constant: 100),
buttonView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -20)
])
scoreLabel.backgroundColor = .blue
newGameBtn.backgroundColor = .yellow
wordToGuess.backgroundColor = .green
buttonView.backgroundColor = .red
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}