Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import PlaygroundSupport
- class MyViewController : UIViewController {
- override func loadView() {
- let view = UIView()
- view.backgroundColor = .white
- self.view = view
- setupStackView()
- }
- func setupStackView() {
- let view = UIView(frame: CGRect(x: 50, y: 50, width: 300, height: 200))
- view.backgroundColor = .gray
- self.view.addSubview(view)
- let hStackView = UIStackView()
- hStackView.translatesAutoresizingMaskIntoConstraints = false
- hStackView.axis = .horizontal
- hStackView.distribution = .equalSpacing
- hStackView.frame = view.bounds
- hStackView.spacing = 10
- view.addSubview(hStackView)
- hStackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true
- hStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 40).isActive = true
- let squareContainer = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
- squareContainer.translatesAutoresizingMaskIntoConstraints = false
- squareContainer.backgroundColor = .green
- // squareContainer.widthAnchor.constraint(equalToConstant: 40)
- let square = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
- square.backgroundColor = .brown
- square.heightAnchor.constraint(equalToConstant: 40).isActive = true
- square.widthAnchor.constraint(equalToConstant: 40).isActive = true
- squareContainer.addSubview(square)
- square.topAnchor.constraint(equalTo: squareContainer.topAnchor).isActive = true
- square.leftAnchor.constraint(equalTo: squareContainer.leftAnchor).isActive = true
- square.rightAnchor.constraint(equalTo: squareContainer.rightAnchor).isActive = true
- hStackView.addArrangedSubview(squareContainer)
- let stackView = UIStackView()
- stackView.axis = .vertical
- stackView.distribution = .equalSpacing
- stackView.spacing = 10
- stackView.setContentHuggingPriority(.defaultHigh, for: .vertical)
- stackView.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
- hStackView.addArrangedSubview(stackView)
- let backgroundView = UIView()
- backgroundView.translatesAutoresizingMaskIntoConstraints = false
- backgroundView.backgroundColor = .yellow
- view.addSubview(backgroundView)
- backgroundView.leftAnchor.constraint(equalTo: hStackView.leftAnchor, constant: -4).isActive = true
- backgroundView.rightAnchor.constraint(equalTo: hStackView.rightAnchor, constant: 4).isActive = true
- backgroundView.topAnchor.constraint(equalTo: hStackView.topAnchor, constant: -4).isActive = true
- backgroundView.bottomAnchor.constraint(equalTo: hStackView.bottomAnchor, constant: 4).isActive = true
- view.bringSubview(toFront: hStackView)
- let grayBackgroundView = UIView()
- grayBackgroundView.translatesAutoresizingMaskIntoConstraints = false
- grayBackgroundView.backgroundColor = .gray
- view.addSubview(grayBackgroundView)
- view.bringSubview(toFront: backgroundView)
- view.bringSubview(toFront: grayBackgroundView)
- view.bringSubview(toFront: hStackView)
- grayBackgroundView.leftAnchor.constraint(equalTo: stackView.leftAnchor, constant: -2).isActive = true
- grayBackgroundView.rightAnchor.constraint(equalTo: stackView.rightAnchor, constant: 2).isActive = true
- grayBackgroundView.topAnchor.constraint(equalTo: stackView.topAnchor, constant: -2).isActive = true
- grayBackgroundView.bottomAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 2).isActive = true
- let label1 = UILabel()
- label1.setContentHuggingPriority(.defaultHigh, for: .vertical)
- label1.backgroundColor = .orange
- label1.text = "111"
- stackView.addArrangedSubview(label1)
- let label2 = UILabel()
- label2.setContentHuggingPriority(.defaultHigh, for: .vertical)
- label2.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
- label2.backgroundColor = .red
- label2.text = "222 444 555"
- stackView.addArrangedSubview(label2)
- let label3 = UILabel()
- label3.setContentHuggingPriority(.defaultHigh, for: .vertical)
- label3.numberOfLines = 0
- label3.backgroundColor = .green
- label3.text = "333\n333"
- stackView.addArrangedSubview(label3)
- let receiptsView = UIView(frame: CGRect(x: 0, y: 100, width: 50, height: 15))
- receiptsView.translatesAutoresizingMaskIntoConstraints = false
- receiptsView.backgroundColor = .white
- view.addSubview(receiptsView)
- receiptsView.widthAnchor.constraint(equalToConstant: 50).isActive = true
- receiptsView.heightAnchor.constraint(equalToConstant: 15).isActive = true
- receiptsView.leftAnchor.constraint(equalTo: stackView.leftAnchor).isActive = true
- receiptsView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 10).isActive = true
- }
- }
- // Present the view controller in the Live View window
- PlaygroundPage.current.liveView = MyViewController()
Add Comment
Please, Sign In to add comment