Untitled

mail@pastecode.io avatar
unknown
swift
a month ago
2.3 kB
4
Indexable
Never
final class SomeView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    
    private func setupView() {
        backgroundColor = .black
        
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.alignment = .center
        stack.translatesAutoresizingMaskIntoConstraints = false
        stack.isLayoutMarginsRelativeArrangement = true
        stack.directionalLayoutMargins = .all(16.0)
        addSubviews(stack)
        NSLayoutConstraint.activate([
            stack.leadingAnchor.constraint(equalTo: leadingAnchor),
            stack.trailingAnchor.constraint(equalTo: trailingAnchor),
            stack.topAnchor.constraint(equalTo: topAnchor),
            stack.bottomAnchor.constraint(equalTo: bottomAnchor)
        ])
        
        let label = UILabel()
        label.textColor = .white
        label.text = "Hello, world"
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        stack.addArrangedSubview(label)
    }
}

final class KekViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        let scroll = UIScrollView()
        scroll.alwaysBounceVertical = true
        scroll.translatesAutoresizingMaskIntoConstraints = false
        scroll.contentInsetAdjustmentBehavior = .never
        view.addSubview(scroll)
        NSLayoutConstraint.activate([
            scroll.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scroll.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            scroll.topAnchor.constraint(equalTo: view.topAnchor),
            scroll.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
        
        let someView = SomeView()
        someView.translatesAutoresizingMaskIntoConstraints = false
        scroll.addSubview(someView)
        NSLayoutConstraint.activate([
            someView.centerXAnchor.constraint(equalTo: scroll.centerXAnchor),
            someView.topAnchor.constraint(equalTo: scroll.topAnchor, constant: 16.0)
        ])
    }
}
Leave a Comment