Guest User

Untitled

a guest
Aug 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. import UIKit
  2. import PlaygroundSupport
  3.  
  4. class MyViewController : UIViewController {
  5. override func loadView() {
  6. let view = UIView()
  7. view.backgroundColor = .white
  8. self.view = view
  9. setupStackView()
  10. }
  11.  
  12. func setupStackView() {
  13. let view = UIView(frame: CGRect(x: 50, y: 50, width: 300, height: 200))
  14. view.backgroundColor = .gray
  15. self.view.addSubview(view)
  16.  
  17. let hStackView = UIStackView()
  18. hStackView.translatesAutoresizingMaskIntoConstraints = false
  19. hStackView.axis = .horizontal
  20. hStackView.distribution = .equalSpacing
  21. hStackView.frame = view.bounds
  22. hStackView.spacing = 10
  23.  
  24. view.addSubview(hStackView)
  25.  
  26. hStackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true
  27. hStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
  28.  
  29. let squareContainer = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
  30. squareContainer.translatesAutoresizingMaskIntoConstraints = false
  31. squareContainer.backgroundColor = .green
  32. // squareContainer.widthAnchor.constraint(equalToConstant: 40)
  33.  
  34. let square = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
  35. square.backgroundColor = .brown
  36. square.heightAnchor.constraint(equalToConstant: 40).isActive = true
  37. square.widthAnchor.constraint(equalToConstant: 40).isActive = true
  38. squareContainer.addSubview(square)
  39. square.topAnchor.constraint(equalTo: squareContainer.topAnchor).isActive = true
  40. square.leftAnchor.constraint(equalTo: squareContainer.leftAnchor).isActive = true
  41. square.rightAnchor.constraint(equalTo: squareContainer.rightAnchor).isActive = true
  42. hStackView.addArrangedSubview(squareContainer)
  43.  
  44. let stackView = UIStackView()
  45. stackView.axis = .vertical
  46. stackView.distribution = .equalSpacing
  47. stackView.spacing = 10
  48. stackView.setContentHuggingPriority(.defaultHigh, for: .vertical)
  49. stackView.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
  50.  
  51. hStackView.addArrangedSubview(stackView)
  52.  
  53.  
  54. let backgroundView = UIView()
  55. backgroundView.translatesAutoresizingMaskIntoConstraints = false
  56. backgroundView.backgroundColor = .yellow
  57. view.addSubview(backgroundView)
  58. backgroundView.leftAnchor.constraint(equalTo: hStackView.leftAnchor, constant: -4).isActive = true
  59. backgroundView.rightAnchor.constraint(equalTo: hStackView.rightAnchor, constant: 4).isActive = true
  60. backgroundView.topAnchor.constraint(equalTo: hStackView.topAnchor, constant: -4).isActive = true
  61. backgroundView.bottomAnchor.constraint(equalTo: hStackView.bottomAnchor, constant: 4).isActive = true
  62.  
  63. view.bringSubview(toFront: hStackView)
  64.  
  65. let grayBackgroundView = UIView()
  66. grayBackgroundView.translatesAutoresizingMaskIntoConstraints = false
  67. grayBackgroundView.backgroundColor = .gray
  68. view.addSubview(grayBackgroundView)
  69. view.bringSubview(toFront: backgroundView)
  70. view.bringSubview(toFront: grayBackgroundView)
  71. view.bringSubview(toFront: hStackView)
  72. grayBackgroundView.leftAnchor.constraint(equalTo: stackView.leftAnchor, constant: -2).isActive = true
  73. grayBackgroundView.rightAnchor.constraint(equalTo: stackView.rightAnchor, constant: 2).isActive = true
  74. grayBackgroundView.topAnchor.constraint(equalTo: stackView.topAnchor, constant: -2).isActive = true
  75. grayBackgroundView.bottomAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 2).isActive = true
  76.  
  77.  
  78. let label1 = UILabel()
  79. label1.setContentHuggingPriority(.defaultHigh, for: .vertical)
  80. label1.backgroundColor = .orange
  81. label1.text = "111"
  82. stackView.addArrangedSubview(label1)
  83.  
  84. let label2 = UILabel()
  85. label2.setContentHuggingPriority(.defaultHigh, for: .vertical)
  86. label2.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
  87. label2.backgroundColor = .red
  88. label2.text = "222 444 555"
  89. stackView.addArrangedSubview(label2)
  90.  
  91. let label3 = UILabel()
  92. label3.setContentHuggingPriority(.defaultHigh, for: .vertical)
  93. label3.numberOfLines = 0
  94. label3.backgroundColor = .green
  95. label3.text = "333\n333"
  96. stackView.addArrangedSubview(label3)
  97.  
  98. let receiptsView = UIView(frame: CGRect(x: 0, y: 100, width: 50, height: 15))
  99. receiptsView.translatesAutoresizingMaskIntoConstraints = false
  100. receiptsView.backgroundColor = .white
  101. view.addSubview(receiptsView)
  102. receiptsView.widthAnchor.constraint(equalToConstant: 50).isActive = true
  103. receiptsView.heightAnchor.constraint(equalToConstant: 15).isActive = true
  104. receiptsView.leftAnchor.constraint(equalTo: stackView.leftAnchor).isActive = true
  105. receiptsView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 10).isActive = true
  106.  
  107. }
  108. }
  109. // Present the view controller in the Live View window
  110. PlaygroundPage.current.liveView = MyViewController()
Add Comment
Please, Sign In to add comment