Don_Mag

Catalyst app with text field and button in stack view

Aug 3rd, 2022
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.54 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  Catalyst1
  4. //
  5. //  Created by Don Mag on 8/3/22.
  6. //
  7.  
  8. import UIKit
  9.  
  10. class ViewController: UIViewController {
  11.  
  12.     let tf = UITextField()
  13.     let infoLabel = UILabel()
  14.    
  15.     override func viewDidLoad() {
  16.         super.viewDidLoad()
  17.        
  18.         let stackV = UIStackView()
  19.         stackV.axis = .vertical
  20.         stackV.spacing = 8
  21.         stackV.backgroundColor = .blue
  22.         stackV.translatesAutoresizingMaskIntoConstraints = false
  23.         view.addSubview(stackV)
  24.        
  25.         let g = view.safeAreaLayoutGuide
  26.         NSLayoutConstraint.activate([
  27.             stackV.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
  28.             stackV.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
  29.             stackV.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
  30.             //stackV.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -40.0),
  31.         ])
  32.        
  33.         tf.borderStyle = .roundedRect
  34.         tf.text = "Text Field"
  35.         stackV.addArrangedSubview(tf)
  36.  
  37.         let btn = UIButton()
  38.         btn.setTitle("Tap Me", for: [])
  39.         btn.setTitleColor(.white, for: .normal)
  40.         btn.setTitleColor(.lightGray, for: .highlighted)
  41.         btn.backgroundColor = .systemRed
  42.         btn.addTarget(self, action: #selector(gotTap(_:)), for: .touchUpInside)
  43.         stackV.addArrangedSubview(btn)
  44.        
  45.         infoLabel.backgroundColor = .cyan
  46.         infoLabel.text = "Info:"
  47.         infoLabel.font = .systemFont(ofSize: 32.0, weight: .light)
  48.         infoLabel.numberOfLines = 0
  49.         stackV.addArrangedSubview(infoLabel)
  50.        
  51.     }
  52.  
  53.     @objc func gotTap(_ sender: Any?) {
  54.         let str = tf.text ?? "Empty"
  55.         infoLabel.text = "Info: \(str)"
  56.     }
  57.  
  58. }
  59.  
Add Comment
Please, Sign In to add comment