Advertisement
Don_Mag

Untitled

Apr 30th, 2019
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.30 KB | None | 0 0
  1. class LabeledSwitchView: UIView {
  2.    
  3.     let theLabel: UILabel = {
  4.         let v = UILabel()
  5.         v.translatesAutoresizingMaskIntoConstraints = false
  6.         v.backgroundColor = .cyan
  7.         v.numberOfLines = 0
  8.         return v
  9.     }()
  10.    
  11.     let theSwitch: UISwitch = {
  12.         let v = UISwitch()
  13.         v.translatesAutoresizingMaskIntoConstraints = false
  14.         return v
  15.     }()
  16.    
  17.     override init(frame: CGRect) {
  18.         super.init(frame: frame)
  19.         commonInit()
  20.     }
  21.    
  22.     required init?(coder aDecoder: NSCoder) {
  23.         super.init(coder: aDecoder)
  24.         commonInit()
  25.     }
  26.    
  27.     func commonInit() -> Void {
  28.        
  29.         addSubview(theLabel)
  30.         addSubview(theSwitch)
  31.        
  32.         NSLayoutConstraint.activate([
  33.            
  34.             // at least 8-pts top and bottom spacing for the label
  35.             theLabel.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: 8.0),
  36.             theLabel.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -8.0),
  37.  
  38.             // at least 8-pts top and bottom spacing for the switch
  39.             theSwitch.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: 8.0),
  40.             theSwitch.bottomAnchor.constraint(lessThanOrEqualTo: bottomAnchor, constant: -8.0),
  41.            
  42.             // 8-pts leading padding for the label
  43.             theLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 8.0),
  44.  
  45.             // 8-pts trailing padding for the switch
  46.             theSwitch.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8.0),
  47.            
  48.             // center the switch vertically with the label
  49.             theSwitch.centerYAnchor.constraint(equalTo: theLabel.centerYAnchor, constant: 0.0),
  50.            
  51.             // 8-pts horizontal between the label and the switch
  52.             theLabel.trailingAnchor.constraint(equalTo: theSwitch.leadingAnchor, constant: -8.0),
  53.            
  54.             ])
  55.        
  56.     }
  57.    
  58. }
  59.  
  60. class HupkesViewController: UIViewController {
  61.    
  62.     let theStack: UIStackView = {
  63.         let v = UIStackView()
  64.         v.translatesAutoresizingMaskIntoConstraints = false
  65.         v.axis = .vertical
  66.         v.alignment = .fill
  67.         v.distribution = .fill
  68.         return v
  69.     }()
  70.  
  71.     let theScrollView: UIScrollView = {
  72.         let v = UIScrollView()
  73.         v.translatesAutoresizingMaskIntoConstraints = false
  74.         v.backgroundColor = .orange
  75.         return v
  76.     }()
  77.    
  78.     let theData = [
  79.         "WordOne",
  80.         "WordOne WordTwo",
  81.         "WordOne WordTwo WordThree",
  82.         "WordOne WordTwo WordThree WordFour",
  83.         "WordOne WordTwo WordThree WordFour WordFive",
  84.         "WordOne WordTwo WordThree WordFour WordFive WordSix",
  85.     ]
  86.    
  87.     override func viewDidLoad() {
  88.         super.viewDidLoad()
  89.        
  90.         view.backgroundColor = UIColor(red: 0.25, green: 0.5, blue: 1.0, alpha: 1.0)
  91.        
  92.         view.addSubview(theScrollView)
  93.         theScrollView.addSubview(theStack)
  94.        
  95.         NSLayoutConstraint.activate([
  96.            
  97.             // constrain the scroll view to top / leading / trailing
  98.             theScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0.0),
  99.             theScrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0.0),
  100.             theScrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0.0),
  101.            
  102.             // short scroll view, so we can get some scrolling
  103.             theScrollView.heightAnchor.constraint(equalToConstant: 240.0),
  104.            
  105.             // constrain the stack view 20-pts top / bottom / leading / trailing
  106.             theStack.topAnchor.constraint(equalTo: theScrollView.topAnchor, constant: 20.0),
  107.             theStack.bottomAnchor.constraint(equalTo: theScrollView.bottomAnchor, constant: -20.0),
  108.             theStack.leadingAnchor.constraint(equalTo: theScrollView.leadingAnchor, constant: 20.0),
  109.             theStack.trailingAnchor.constraint(equalTo: theScrollView.trailingAnchor, constant: -20.0),
  110.            
  111.             // constrain stack view width equal to scroll view width - 40 (20-pts on each side)
  112.             theStack.widthAnchor.constraint(equalTo: theScrollView.widthAnchor, multiplier: 1.0, constant: -40.0),
  113.            
  114.             ])
  115.        
  116.         // create and add the custom views to the stack view
  117.         theData.forEach { s in
  118.             let v = LabeledSwitchView()
  119.             v.translatesAutoresizingMaskIntoConstraints = false
  120.             v.backgroundColor = .white
  121.             v.theLabel.text = s
  122.             v.theSwitch.isOn = true
  123.             theStack.addArrangedSubview(v)
  124.            
  125.             // if it's not the last one, add a "separator line"
  126.             if s != theData.last {
  127.                 let v = UIView()
  128.                 v.translatesAutoresizingMaskIntoConstraints = false
  129.                 v.backgroundColor = .lightGray
  130.                 v.heightAnchor.constraint(equalToConstant: 1.0).isActive = true
  131.                 theStack.addArrangedSubview(v)
  132.             }
  133.         }
  134.        
  135.     }
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement