|
| 1 | +// |
| 2 | +// MarginViewController.swift |
| 3 | +// Margins |
| 4 | +// |
| 5 | +// Created by Keith Harrison https://useyourloaf.com |
| 6 | +// Copyright (c) 2017 Keith Harrison. All rights reserved. |
| 7 | +// |
| 8 | +// Redistribution and use in source and binary forms, with or without |
| 9 | +// modification, are permitted provided that the following conditions are met: |
| 10 | +// |
| 11 | +// 1. Redistributions of source code must retain the above copyright |
| 12 | +// notice, this list of conditions and the following disclaimer. |
| 13 | +// |
| 14 | +// 2. Redistributions in binary form must reproduce the above copyright |
| 15 | +// notice, this list of conditions and the following disclaimer in the |
| 16 | +// documentation and/or other materials provided with the distribution. |
| 17 | +// |
| 18 | +// 3. Neither the name of the copyright holder nor the names of its |
| 19 | +// contributors may be used to endorse or promote products derived from |
| 20 | +// this software without specific prior written permission. |
| 21 | +// |
| 22 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 26 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | +// POSSIBILITY OF SUCH DAMAGE. |
| 33 | + |
| 34 | +import UIKit |
| 35 | + |
| 36 | +public class MarginViewController: UIViewController { |
| 37 | + |
| 38 | + override public func viewDidLoad() { |
| 39 | + super.viewDidLoad() |
| 40 | + setupViews() |
| 41 | + } |
| 42 | + |
| 43 | + public lazy var redView: UIView = { |
| 44 | + return self.simpleView(color: .red) |
| 45 | + }() |
| 46 | + |
| 47 | + public lazy var stackView: UIStackView = { |
| 48 | + return UIStackView() |
| 49 | + }() |
| 50 | + |
| 51 | + private func setupViews() { |
| 52 | + view.addSubview(redView) |
| 53 | + NSLayoutConstraint.activate([ |
| 54 | + redView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor), |
| 55 | + redView.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 56 | + redView.trailingAnchor.constraint(equalTo: view.trailingAnchor) |
| 57 | + ]) |
| 58 | + |
| 59 | + let yellowView = simpleView(color: .yellow) |
| 60 | + redView.addSubview(yellowView) |
| 61 | + let redMargins = redView.layoutMarginsGuide |
| 62 | + NSLayoutConstraint.activate([ |
| 63 | + yellowView.leadingAnchor.constraint(equalTo: redMargins.leadingAnchor), |
| 64 | + yellowView.trailingAnchor.constraint(equalTo: redMargins.trailingAnchor), |
| 65 | + yellowView.topAnchor.constraint(equalTo: redMargins.topAnchor), |
| 66 | + yellowView.bottomAnchor.constraint(equalTo: redMargins.bottomAnchor) |
| 67 | + ]) |
| 68 | + |
| 69 | + let blueView = simpleView(color: .blue) |
| 70 | + stackView.addArrangedSubview(blueView) |
| 71 | + let greenView = simpleView(color: .green) |
| 72 | + stackView.addArrangedSubview(greenView) |
| 73 | + stackView.spacing = 8.0 |
| 74 | + stackView.distribution = .fillEqually |
| 75 | + stackView.translatesAutoresizingMaskIntoConstraints = false |
| 76 | + view.addSubview(stackView) |
| 77 | + |
| 78 | + NSLayoutConstraint.activate([ |
| 79 | + stackView.topAnchor.constraint(equalTo: redView.bottomAnchor), |
| 80 | + stackView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor), |
| 81 | + stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 82 | + stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor), |
| 83 | + |
| 84 | + stackView.heightAnchor.constraint(equalTo: redView.heightAnchor) |
| 85 | + ]) |
| 86 | + } |
| 87 | + |
| 88 | + private func simpleView(color: UIColor) -> UIView { |
| 89 | + let view = UIView() |
| 90 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 91 | + view.backgroundColor = color |
| 92 | + return view |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments