Advertisement
Don_Mag

Untitled

Dec 3rd, 2019
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.57 KB | None | 0 0
  1. /*
  2. Here is some sample code for a quick test of adding 48 labels (8 to each of 6 stack views).
  3.  
  4. The button at the top toggles between adding / removing the labels, so it can be run multiple times.
  5.  
  6. On an iPad Air (`ME991LL/A`), I get 0.03 seconds or less.
  7. */
  8.  
  9. class ViewController: UIViewController {
  10.    
  11.     let theBtn: UIButton = {
  12.         let v = UIButton()
  13.         v.translatesAutoresizingMaskIntoConstraints = false
  14.         v.setTitle("Add Views", for: .normal)
  15.         v.setContentHuggingPriority(.required, for: .vertical)
  16.         v.backgroundColor = .red
  17.         return v
  18.     }()
  19.    
  20.     let promptLabel: UILabel = {
  21.         let v = UILabel()
  22.         v.translatesAutoresizingMaskIntoConstraints = false
  23.         v.textAlignment = .center
  24.         v.text = "Tap button to:"
  25.         return v
  26.     }()
  27.    
  28.     let statLabel: UILabel = {
  29.         let v = UILabel()
  30.         v.translatesAutoresizingMaskIntoConstraints = false
  31.         v.backgroundColor = .cyan
  32.         v.textAlignment = .center
  33.         v.text = ""
  34.         return v
  35.     }()
  36.    
  37.     let topStackView: UIStackView = {
  38.         let v = UIStackView()
  39.         v.axis = .vertical
  40.         v.distribution = .fillEqually
  41.         v.spacing = 4
  42.         v.translatesAutoresizingMaskIntoConstraints = false
  43.         return v
  44.     }()
  45.    
  46.     let hStackView: UIStackView = {
  47.         let v = UIStackView()
  48.         v.axis = .horizontal
  49.         v.distribution = .fillEqually
  50.         v.spacing = 8
  51.         v.translatesAutoresizingMaskIntoConstraints = false
  52.         return v
  53.     }()
  54.  
  55.     var stackViews: [UIStackView] = [UIStackView]()
  56.    
  57.     var jCounter = 1
  58.  
  59.     override func viewDidLoad() {
  60.         super.viewDidLoad()
  61.        
  62.         view.backgroundColor = .white
  63.        
  64.         for _ in 1...6 {
  65.             let v = UIStackView()
  66.             v.axis = .vertical
  67.             v.distribution = .fillEqually
  68.             v.spacing = 8
  69.             v.translatesAutoresizingMaskIntoConstraints = false
  70.             stackViews.append(v)
  71.         }
  72.        
  73.         topStackView.addArrangedSubview(promptLabel)
  74.         topStackView.addArrangedSubview(theBtn)
  75.         topStackView.addArrangedSubview(statLabel)
  76.  
  77.         view.addSubview(topStackView)
  78.         view.addSubview(hStackView)
  79.        
  80.         let g = view.safeAreaLayoutGuide
  81.        
  82.         NSLayoutConstraint.activate([
  83.            
  84.             topStackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  85.             topStackView.centerXAnchor.constraint(equalTo: g.centerXAnchor, constant: 0.0),
  86.             topStackView.widthAnchor.constraint(equalToConstant: 300.0),
  87.            
  88.             hStackView.topAnchor.constraint(equalTo: topStackView.bottomAnchor, constant: 20.0),
  89.             hStackView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -8.0),
  90.             hStackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 8.0),
  91.             hStackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -8.0),
  92.  
  93.         ])
  94.        
  95.         stackViews.forEach {
  96.             hStackView.addArrangedSubview($0)
  97.         }
  98.        
  99.         theBtn.addTarget(self, action: #selector(addViews(_:)), for: .touchUpInside)
  100.        
  101.     }
  102.    
  103.     func doAddViews() -> Void {
  104.  
  105.         let start = CFAbsoluteTimeGetCurrent()
  106.        
  107.         stackViews.forEach {
  108.             sv in
  109.             for _ in 1...8 {
  110.                 let v = UILabel()
  111.                 v.font = UIFont.systemFont(ofSize: 10.0, weight: .medium)
  112.                 v.backgroundColor = .yellow
  113.                 v.textAlignment = .center
  114.                 v.text = "\(jCounter)"
  115.                 sv.addArrangedSubview(v)
  116.                 jCounter += 1
  117.             }
  118.         }
  119.        
  120.         let end = CFAbsoluteTimeGetCurrent() - start
  121.        
  122.         statLabel.text = "Elapsed Time: \(end)"
  123.        
  124.     }
  125.    
  126.     @objc func addViews(_ sender: Any?) -> Void {
  127.  
  128.         if stackViews.first?.arrangedSubviews.count == 0 {
  129.  
  130.             doAddViews()
  131.            
  132.             theBtn.setTitle("Clear Views", for: .normal)
  133.  
  134.         } else {
  135.  
  136.             stackViews.forEach {
  137.                 sv in
  138.                 sv.arrangedSubviews.forEach {
  139.                     $0.removeFromSuperview()
  140.                 }
  141.             }
  142.  
  143.             statLabel.text = ""
  144.  
  145.             theBtn.setTitle("Add Views", for: .normal)
  146.  
  147.         }
  148.        
  149.     }
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement