Guest User

hangman constraint issue

a guest
Jul 11th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.76 KB | None | 0 0
  1. class ViewController: UIViewController {
  2.    
  3.     // letterGuess
  4.     // usedLetters
  5.     // score/lives
  6.    
  7.     var scoreLabel: UILabel!
  8.     var answerLabel: UILabel!
  9.     var characterButtons = [UIButton]()
  10.    
  11.     let width:CGFloat = 100
  12.     let height:CGFloat = 100
  13.    
  14.     var score = 0 {
  15.         didSet {
  16.             scoreLabel.text = "Score: \(score)"
  17.         }
  18.     }
  19.     override func loadView() {
  20.         view = UIView()
  21.         view.backgroundColor = .white
  22.        
  23.         scoreLabel = UILabel()
  24.         scoreLabel.translatesAutoresizingMaskIntoConstraints = false
  25.         scoreLabel.textAlignment = .right
  26.         scoreLabel.font = UIFont.systemFont(ofSize: 24)
  27.         scoreLabel.text = "Score: 0"
  28.         view.addSubview(scoreLabel)
  29.        
  30.         answerLabel = UILabel()
  31.         answerLabel.translatesAutoresizingMaskIntoConstraints = false
  32.         answerLabel.font = UIFont.systemFont(ofSize: 24)
  33.         answerLabel.text = "ANSWER"
  34.         answerLabel.numberOfLines = 1
  35.         answerLabel.textAlignment = .center
  36.         answerLabel.setContentHuggingPriority(UILayoutPriority(1), for: .vertical)
  37.         view.addSubview(answerLabel)
  38.        
  39.         let buttonsView = UIView()
  40.         buttonsView.translatesAutoresizingMaskIntoConstraints = false
  41.         view.addSubview(buttonsView)
  42.        
  43.         let row1View = UIView()
  44.         row1View.clipsToBounds = true
  45.         row1View.translatesAutoresizingMaskIntoConstraints = false
  46.         buttonsView.addSubview(row1View)
  47.        
  48.         let row2View = UIView()
  49.         row2View.clipsToBounds = true
  50.        
  51.         row2View.translatesAutoresizingMaskIntoConstraints = false
  52.         row2View.setContentHuggingPriority(.defaultLow, for: .vertical)
  53.         buttonsView.addSubview(row2View)
  54.        
  55.         let row3View = UIView()
  56.         row3View.clipsToBounds = true
  57.        
  58.         row3View.translatesAutoresizingMaskIntoConstraints = false
  59.         buttonsView.addSubview(row3View)
  60.        
  61.        
  62.         NSLayoutConstraint.activate([
  63.             scoreLabel.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
  64.             scoreLabel.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: 0),
  65.            
  66.             answerLabel.topAnchor.constraint(equalTo: scoreLabel.bottomAnchor, constant: 25),
  67.             answerLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  68.            
  69.             buttonsView.widthAnchor.constraint(equalToConstant: 1000),
  70.             buttonsView.heightAnchor.constraint(equalToConstant: 300),
  71.             buttonsView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  72.             buttonsView.topAnchor.constraint(equalTo: answerLabel.bottomAnchor, constant: 20),
  73.             buttonsView.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor, constant: -20),
  74.            
  75.             row1View.leftAnchor.constraint(equalTo: buttonsView.leftAnchor),
  76.             row1View.topAnchor.constraint(equalTo: buttonsView.topAnchor),
  77.             row1View.widthAnchor.constraint(equalTo: buttonsView.widthAnchor),
  78.             row1View.heightAnchor.constraint(equalToConstant: height),
  79.            
  80.             row2View.leftAnchor.constraint(equalTo: buttonsView.leftAnchor),
  81.             row2View.topAnchor.constraint(equalTo: row1View.bottomAnchor),
  82.             row2View.widthAnchor.constraint(equalTo: buttonsView.widthAnchor),
  83.             row2View.heightAnchor.constraint(equalToConstant: height),
  84.  
  85.             row3View.leftAnchor.constraint(equalTo: buttonsView.leftAnchor),
  86.             row3View.topAnchor.constraint(equalTo: row2View.bottomAnchor),
  87.             row3View.widthAnchor.constraint(equalTo: buttonsView.widthAnchor),
  88.             row3View.heightAnchor.constraint(equalToConstant: height)
  89.            
  90.            
  91.         ])
  92.        
  93.        
  94.         var i = 10
  95.        
  96.         for row in 0..<3 {
  97.             print(row)
  98.             switch row {
  99.             case 0:
  100.                 i = 10
  101.             case 1:
  102.                 i = 9
  103.             case 2:
  104.                 i = 7
  105.             default:
  106.                 return
  107.             }
  108.             for col in 0..<i {
  109.                 let characterButton = UIButton(type: .system)
  110.                 characterButton.titleLabel?.font = UIFont.systemFont(ofSize: 36)
  111.                
  112.                 characterButton.layer.borderWidth = 1
  113.                 characterButton.layer.borderColor = UIColor.lightGray.cgColor
  114.                 characterButton.layer.backgroundColor = UIColor.white.cgColor
  115.                 characterButton.setTitle("#", for: .normal)
  116.                
  117.                 let frame = CGRect(x: CGFloat(col) * width, y: 0, width: width, height: height)
  118.                 characterButton.frame = frame
  119.                
  120.                 switch row {
  121.                 case 0:
  122.                     print(row)
  123.                     print("row 1")
  124.                     row1View.addSubview(characterButton)
  125.                 case 1:
  126.                     print(row)
  127.                     print("row 2")
  128.                     row2View.addSubview(characterButton)
  129.                 case 2:
  130.                     print(row)
  131.                     print("row 3")
  132.                     row3View.addSubview(characterButton)
  133.                 default:
  134.                     print("defualt")
  135.                     return
  136.                 }
  137.                
  138.                 characterButton.addTarget(self, action: #selector(buttonFunction), for: .touchUpInside)
  139.             }
  140.         }
  141.        
  142.         buttonsView.backgroundColor = .purple
  143.         row1View.backgroundColor = .red
  144.         row2View.backgroundColor = .yellow
  145.         row3View.backgroundColor = .green
  146.        
  147.     }
  148.    
  149.     @objc func buttonFunction(){
  150.         //your code goes here
  151.     }
  152.    
  153. }
Add Comment
Please, Sign In to add comment