Advertisement
Don_Mag

Untitled

Aug 26th, 2024 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.93 KB | None | 0 0
  1. // using a UIScrollView
  2.  
  3. class ViewController: UIViewController {
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         view.backgroundColor = .systemBackground
  9.        
  10.         let scrollView = UIScrollView()
  11.         scrollView.translatesAutoresizingMaskIntoConstraints = false
  12.         scrollView.showsVerticalScrollIndicator = false
  13.         scrollView.showsHorizontalScrollIndicator = false
  14.         scrollView.bounces = true
  15.         self.view.addSubview(scrollView)
  16.        
  17.         NSLayoutConstraint.activate([
  18.             scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
  19.             scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
  20.             scrollView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 20),
  21.             scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
  22.         ])
  23.        
  24.         // Create the UILabels, UIImageView, and UIButton
  25.         let titleLabel = UILabel()
  26.         titleLabel.text = "Your Intelligence Types"
  27.         titleLabel.font = UIFont(name: "Inter-Bold", size: 20)
  28.         titleLabel.textAlignment = .center
  29.        
  30.         // Create the UILabels, UIImageView, and UIButton
  31.         let descriptionLbl = UILabel()
  32.         descriptionLbl.text = "Different types of intelligence highlight the diverse ways people think, solve problems, and interact with the world around them."
  33.         descriptionLbl.font = UIFont(name: "Inter-Regular", size: 14)
  34.         descriptionLbl.textAlignment = .left
  35.         descriptionLbl.numberOfLines = 0
  36.        
  37.         titleLabel.translatesAutoresizingMaskIntoConstraints = false
  38.         scrollView.addSubview(titleLabel)
  39.        
  40.         descriptionLbl.translatesAutoresizingMaskIntoConstraints = false
  41.         scrollView.addSubview(descriptionLbl)
  42.        
  43.         // so we can see the framing
  44.         scrollView.backgroundColor = .init(white: 0.95, alpha: 1.0)
  45.         titleLabel.backgroundColor = .cyan
  46.         descriptionLbl.backgroundColor = .green
  47.  
  48.         let cg = scrollView.contentLayoutGuide
  49.         let fg = scrollView.frameLayoutGuide
  50.        
  51.         // Set constraints for the stackView
  52.         NSLayoutConstraint.activate([
  53.             titleLabel.topAnchor.constraint(equalTo: cg.topAnchor, constant: 20),
  54.             titleLabel.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 20),
  55.            
  56.             // don't set a trailing anchor
  57.             //titleLabel.trailingAnchor.constraint(equalTo: cg.trailingAnchor, constant: -20),
  58.            
  59.             // set the title label width to the width of the scrollView's frame layout guide
  60.             //  minus 40-points (20-points on each side)
  61.             titleLabel.widthAnchor.constraint(equalTo: fg.widthAnchor, constant: -40.0),
  62.            
  63.             titleLabel.heightAnchor.constraint(equalToConstant: 30)
  64.         ])
  65.        
  66.         // Set constraints for the stackView
  67.         NSLayoutConstraint.activate([
  68.             descriptionLbl.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
  69.             descriptionLbl.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 20),
  70.            
  71.             // don't hard-code the width
  72.             //descriptionLbl.widthAnchor.constraint(equalToConstant: 350),
  73.            
  74.             // set the descriptionLbl label width to the width of the scrollView's frame layout guide
  75.             //  minus 40-points (20-points on each side)
  76.             descriptionLbl.widthAnchor.constraint(equalTo: fg.widthAnchor, constant: -40.0),
  77.            
  78.             // if you want the description label height to "auto-size"
  79.             //  don't set the height here
  80.             //descriptionLbl.heightAnchor.constraint(equalToConstant: 60)
  81.         ])
  82.        
  83.  
  84.         var previousProgressView : UIProgressView? = nil
  85.         let previousTitleHeight: CGFloat = 30
  86.         let progressViewHeight: CGFloat = 20
  87.         let labelHeight: CGFloat = 20
  88.         let verticalSpacing: CGFloat = 20
  89.        
  90.         var n = 0;
  91.        
  92.         // I don't have your data, so let's just add 20 progress view's (witht their labels)
  93.         //for (key, value) in totalTypes {
  94.         for i in 0..<20 {
  95.             let typeLabel = UILabel()
  96.            
  97.             //typeLabel.text = key
  98.             typeLabel.text = "key \(i)"
  99.            
  100.             typeLabel.font = UIFont(name: "Inter-Bold", size: 16)
  101.             typeLabel.textAlignment = .left
  102.             typeLabel.backgroundColor = .yellow
  103.            
  104.             typeLabel.translatesAutoresizingMaskIntoConstraints = false
  105.            
  106.             let iprogressView = UIProgressView(progressViewStyle: .bar)
  107.             iprogressView.trackTintColor = UIColor(red: 0.07, green: 0.45, blue: 0.87, alpha: 0.1)
  108.             iprogressView.tintColor = UIColor(red: 0.07, green: 0.45, blue: 0.87, alpha: 1.00)
  109.             iprogressView.layer.cornerRadius = 4
  110.             iprogressView.clipsToBounds = true
  111.            
  112.             //iprogressView.setProgress(Float(userTypes[key] ?? 0) / Float(value), animated: true)
  113.             iprogressView.setProgress(Float(i) * 0.05, animated: true)
  114.            
  115.             iprogressView.translatesAutoresizingMaskIntoConstraints = false
  116.            
  117.             let transform : CGAffineTransform = CGAffineTransform(scaleX: 1.0, y: 3.0)
  118.             iprogressView.transform = transform
  119.            
  120.             let progressLabel = UILabel()
  121.            
  122.             //let progressValue = round(((Float(userTypes[key] ?? 0) / Float(value)) * 100) * 100) / 100
  123.             let progressValue = String(format: "%0.2f", Float(i) * 0.05 * 100.0)
  124.             progressLabel.text = "\(progressValue) %"
  125.            
  126.             progressLabel.translatesAutoresizingMaskIntoConstraints = false
  127.             progressLabel.font = UIFont(name: "Inter-Regular", size: 16)
  128.             progressLabel.backgroundColor = .systemYellow
  129.            
  130.             scrollView.addSubview(typeLabel)
  131.             scrollView.addSubview(iprogressView)
  132.             scrollView.addSubview(progressLabel)
  133.            
  134.             NSLayoutConstraint.activate([
  135.                 typeLabel.topAnchor.constraint(equalTo: previousProgressView?.bottomAnchor ?? descriptionLbl.bottomAnchor, constant: previousProgressView == nil ? verticalSpacing : verticalSpacing),
  136.                 typeLabel.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 20),
  137.                 //typeLabel.trailingAnchor.constraint(equalTo: cg.trailingAnchor, constant: -20),
  138.                 typeLabel.heightAnchor.constraint(equalToConstant: 20),
  139.                
  140.                 iprogressView.topAnchor.constraint(equalTo: typeLabel.bottomAnchor, constant: 20),
  141.                 iprogressView.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 20),
  142.                 iprogressView.widthAnchor.constraint(equalToConstant: 200),
  143.                 iprogressView.heightAnchor.constraint(equalToConstant: 3),
  144.                
  145.                 // Position the label next to the progressView
  146.                 progressLabel.leadingAnchor.constraint(equalTo: iprogressView.trailingAnchor, constant: 10),
  147.                 progressLabel.centerYAnchor.constraint(equalTo: iprogressView.centerYAnchor)
  148.             ])
  149.            
  150.             // Update the previousProgressView reference
  151.             previousProgressView = iprogressView
  152.             n += 1
  153.         }
  154.        
  155.         // we MUST have a bottom constraint to let the scrollView know it has content to scroll
  156.         previousProgressView?.bottomAnchor.constraint(equalTo: cg.bottomAnchor, constant: -20.0).isActive = true
  157.        
  158.         // we're using auto-layout / constraints ... don't do this
  159.         // Set the content size of the scrollView
  160.         //let totalContentHeight = (CGFloat(totalTypes.count) * (labelHeight + progressViewHeight + verticalSpacing + CGFloat(100))) + verticalSpacing + CGFloat(2000)
  161.         //scrollView.contentSize = CGSize(width: self.view.frame.width, height: totalContentHeight)
  162.  
  163.     }
  164. }
  165.  
  166. // NOTE: based on the layout, this code assumes we want vertical scrolling only... so, no need to set constraints to the content layout guide trailing.
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement