UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Milestone 4: Setting NSLayoutConstraints

Forums > 100 Days of Swift

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.
    }
}

3      

Hi! My guess is that this:

 wordToGuess.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor, constant: 100),

Is pulling the scoreLabel down. You can try the greaterOrEqualThan variant (or whatever is the precise name) :-)

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.