Skip to content

Commit c30aca3

Browse files
committed
Add Margins
1 parent 7bd8c60 commit c30aca3

File tree

12 files changed

+718
-0
lines changed

12 files changed

+718
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import UIKit
2+
import PlaygroundSupport
3+
4+
let viewController = MarginViewController()
5+
viewController.view.backgroundColor = .white
6+
viewController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 640)
7+
PlaygroundPage.current.liveView = viewController.view
8+
9+
viewController.redView.preservesSuperviewLayoutMargins = true
10+
11+
viewController.stackView.preservesSuperviewLayoutMargins = true
12+
viewController.stackView.isLayoutMarginsRelativeArrangement = true
13+
14+
viewController.stackView.layoutMargins = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

0 commit comments

Comments
 (0)