-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric Hyche
committed
Aug 5, 2017
1 parent
b80ee74
commit 480d23b
Showing
41 changed files
with
1,694 additions
and
15 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
...nAboutAutoLayout.playground/Pages/Introducing Constraints.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import UIKit | ||
import PlaygroundSupport | ||
|
||
let liveView = EHAdjustableContainerView(frame: CGRect(x: 0.0, y: 0.0, width: 512.0, height: 512.0)) | ||
|
||
let label0 = UILabel(frame: .zero) | ||
label0.translatesAutoresizingMaskIntoConstraints = false | ||
label0.backgroundColor = .green | ||
label0.text = "111" | ||
label0.textColor = .white | ||
label0.font = UIFont.systemFont(ofSize: 18.0) | ||
|
||
liveView.containerView.addSubview(label0) | ||
|
||
let label1 = UILabel(frame: .zero) | ||
label1.translatesAutoresizingMaskIntoConstraints = false | ||
label1.backgroundColor = .red | ||
label1.text = "22222" | ||
label1.textColor = .white | ||
label1.font = UIFont.systemFont(ofSize: 24.0) | ||
|
||
liveView.containerView.addSubview(label1) | ||
|
||
label0.centerYAnchor.constraint(equalTo: liveView.containerView.centerYAnchor).isActive = true | ||
label1.centerYAnchor.constraint(equalTo: liveView.containerView.centerYAnchor).isActive = true | ||
|
||
let horzConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-(20)-[label0]-(20)-[label1]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label0": label0, "label1": label1]) | ||
NSLayoutConstraint.activate(horzConstraints) | ||
|
||
|
||
PlaygroundPage.current.needsIndefiniteExecution = true | ||
PlaygroundPage.current.liveView = liveView | ||
|
6 changes: 6 additions & 0 deletions
6
...tAutoLayout.playground/Pages/Introducing Constraints.xcplaygroundpage/timeline.xctimeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |
7 changes: 7 additions & 0 deletions
7
LearnAboutAutoLayout.playground/Pages/Untitled Page 2.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//: [Previous](@previous) | ||
|
||
import Foundation | ||
|
||
var str = "Hello, playground" | ||
|
||
//: [Next](@next) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+208 KB
LearnAboutAutoLayout.playground/Resources/OpenSans-ExtraBoldItalic.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
80 changes: 80 additions & 0 deletions
80
LearnAboutAutoLayout.playground/Sources/EHAdjustableContainerView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import UIKit | ||
|
||
public class EHAdjustableContainerView : UIView { | ||
|
||
// MARK: - Public properties | ||
|
||
public let containerView = UIView(frame: .zero) | ||
|
||
// MARK: - Private properties | ||
|
||
private let internalContainerView = UIView(frame: .zero) | ||
private let containerSizeInfoView = EHContainerSizeInfoView(frame: .zero) | ||
private var containerViewWidthConstraint = NSLayoutConstraint() | ||
private var containerViewHeightConstraint = NSLayoutConstraint() | ||
|
||
// MARK: - Initializers | ||
|
||
override public init(frame: CGRect) { | ||
super.init(frame: frame) | ||
setupView() | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: - UIView methods | ||
|
||
override public func layoutSubviews() { | ||
super.layoutSubviews() | ||
|
||
containerSizeInfoView.maxContainerViewSize = internalContainerView.frame.size | ||
} | ||
|
||
|
||
// MARK: - Private methods | ||
|
||
func setupView() { | ||
backgroundColor = UIColor.white | ||
|
||
internalContainerView.translatesAutoresizingMaskIntoConstraints = false | ||
internalContainerView.backgroundColor = UIColor.white | ||
internalContainerView.layer.borderColor = UIColor.black.cgColor | ||
internalContainerView.layer.borderWidth = 2.0 | ||
addSubview(internalContainerView) | ||
|
||
containerView.translatesAutoresizingMaskIntoConstraints = false | ||
containerView.backgroundColor = UIColor.lightGray | ||
internalContainerView.addSubview(containerView) | ||
|
||
containerView.centerXAnchor.constraint(equalTo: internalContainerView.centerXAnchor).isActive = true | ||
containerView.centerYAnchor.constraint(equalTo: internalContainerView.centerYAnchor).isActive = true | ||
|
||
containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: 256.0) | ||
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: 256.0) | ||
containerViewWidthConstraint.isActive = true | ||
containerViewHeightConstraint.isActive = true | ||
|
||
containerSizeInfoView.translatesAutoresizingMaskIntoConstraints = false | ||
containerSizeInfoView.backgroundColor = UIColor.white | ||
addSubview(containerSizeInfoView) | ||
|
||
internalContainerView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10.0).isActive = true | ||
trailingAnchor.constraint(equalTo: internalContainerView.trailingAnchor, constant: 10.0).isActive = true | ||
internalContainerView.topAnchor.constraint(equalTo: topAnchor, constant: 10.0).isActive = true | ||
|
||
containerSizeInfoView.topAnchor.constraint(equalTo: internalContainerView.bottomAnchor, constant: 10.0).isActive = true | ||
|
||
containerSizeInfoView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10.0).isActive = true | ||
trailingAnchor.constraint(equalTo: containerSizeInfoView.trailingAnchor, constant: 10.0).isActive = true | ||
bottomAnchor.constraint(equalTo: containerSizeInfoView.bottomAnchor, constant: 10.0).isActive = true | ||
|
||
containerSizeInfoView.containerViewSizeChanged = { [weak self] (size) in | ||
self?.containerViewWidthConstraint.constant = size.width | ||
self?.containerViewHeightConstraint.constant = size.height | ||
} | ||
} | ||
|
||
} | ||
|
143 changes: 143 additions & 0 deletions
143
LearnAboutAutoLayout.playground/Sources/EHContainerSizeInfoView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import UIKit | ||
|
||
public typealias EHContainerViewSizeChangedBlock = (CGSize) -> Void | ||
|
||
public class EHContainerSizeInfoView: UIView { | ||
|
||
// MARK: - Public properties | ||
public let minContainerViewSize = CGSize(width: 40.0, height: 40.0) | ||
public var maxContainerViewSize = CGSize(width: 256.0, height: 256.0) { | ||
didSet { | ||
if maxContainerViewSize != oldValue { | ||
updateWidth() | ||
updateHeight() | ||
} | ||
} | ||
} | ||
public var containerViewSize = CGSize(width: 256.0, height: 128.0) { | ||
didSet { | ||
if containerViewSize != oldValue { | ||
containerViewSizeChanged?(containerViewSize) | ||
} | ||
} | ||
} | ||
public var containerViewSizeChanged: EHContainerViewSizeChangedBlock? | ||
|
||
// MARK: - Private properties | ||
private let widthSlider = UISlider(frame: .zero) | ||
private let heightSlider = UISlider(frame: .zero) | ||
private let widthLabel = UILabel(frame: .zero) | ||
private let heightLabel = UILabel(frame: .zero) | ||
private let verticalStackView = UIStackView(frame: .zero) | ||
private let widthStackView = UIStackView(frame: .zero) | ||
private let heightStackView = UIStackView(frame: .zero) | ||
|
||
// MARK: - Initializers | ||
override public init(frame: CGRect) { | ||
super.init(frame: frame) | ||
setupViews() | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: - Private methods | ||
private func setupViews() { | ||
|
||
// Set up the width horizontal stack view | ||
widthStackView.translatesAutoresizingMaskIntoConstraints = false | ||
widthStackView.axis = .horizontal | ||
widthStackView.distribution = .fill | ||
widthStackView.alignment = .center | ||
widthStackView.spacing = 10.0 | ||
|
||
widthSlider.translatesAutoresizingMaskIntoConstraints = false | ||
widthSlider.minimumValue = 0.0 | ||
widthSlider.maximumValue = 1.0 | ||
widthSlider.value = 1.0 | ||
widthSlider.isContinuous = true | ||
widthSlider.addTarget(self, action: #selector(widthSliderChanged(slider:)), for: .valueChanged) | ||
|
||
widthStackView.addArrangedSubview(widthSlider) | ||
|
||
widthLabel.translatesAutoresizingMaskIntoConstraints = false | ||
widthLabel.font = UIFont.systemFont(ofSize: 16.0) | ||
widthLabel.textAlignment = .right | ||
widthLabel.textColor = UIColor.black | ||
widthLabel.text = "\(Int(computedWidth()))" | ||
|
||
widthStackView.addArrangedSubview(widthLabel) | ||
|
||
// Set up the height horizontal stack view | ||
heightStackView.translatesAutoresizingMaskIntoConstraints = false | ||
heightStackView.axis = .horizontal | ||
heightStackView.distribution = .fill | ||
heightStackView.alignment = .center | ||
heightStackView.spacing = 10.0 | ||
|
||
heightSlider.translatesAutoresizingMaskIntoConstraints = false | ||
heightSlider.minimumValue = 0.0 | ||
heightSlider.maximumValue = 1.0 | ||
heightSlider.value = 1.0 | ||
heightSlider.isContinuous = true | ||
heightSlider.addTarget(self, action: #selector(heightSliderChanged(slider:)), for: .valueChanged) | ||
|
||
heightStackView.addArrangedSubview(heightSlider) | ||
|
||
heightLabel.translatesAutoresizingMaskIntoConstraints = false | ||
heightLabel.font = UIFont.systemFont(ofSize: 16.0) | ||
heightLabel.textAlignment = .right | ||
heightLabel.textColor = UIColor.black | ||
heightLabel.text = "\(Int(computedHeight()))" | ||
|
||
heightStackView.addArrangedSubview(heightLabel) | ||
|
||
// Now set up the vertical stack view | ||
|
||
verticalStackView.translatesAutoresizingMaskIntoConstraints = false | ||
verticalStackView.axis = .vertical | ||
verticalStackView.distribution = .fill | ||
verticalStackView.alignment = .fill | ||
verticalStackView.spacing = 10.0 | ||
|
||
verticalStackView.addArrangedSubview(widthStackView) | ||
verticalStackView.addArrangedSubview(heightStackView) | ||
|
||
addSubview(verticalStackView) | ||
|
||
verticalStackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true | ||
verticalStackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true | ||
verticalStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true | ||
verticalStackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | ||
|
||
} | ||
|
||
@objc private func widthSliderChanged(slider: UISlider) { | ||
updateWidth() | ||
} | ||
|
||
@objc private func heightSliderChanged(slider: UISlider) { | ||
updateHeight() | ||
} | ||
|
||
private func computedWidth() -> CGFloat { | ||
return (maxContainerViewSize.width - minContainerViewSize.width) * CGFloat(widthSlider.value) + minContainerViewSize.width | ||
} | ||
|
||
private func computedHeight() -> CGFloat { | ||
return (maxContainerViewSize.height - minContainerViewSize.height) * CGFloat(heightSlider.value) + minContainerViewSize.height | ||
} | ||
|
||
private func updateWidth() { | ||
let width = computedWidth() | ||
widthLabel.text = "\(Int(width))" | ||
containerViewSize = CGSize(width: width, height: containerViewSize.height) | ||
} | ||
|
||
private func updateHeight() { | ||
let height = computedHeight() | ||
heightLabel.text = "\(Int(height))" | ||
containerViewSize = CGSize(width: containerViewSize.width, height: height) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='6.0' target-platform='ios'> | ||
<pages> | ||
<page name='Introducing Constraints'/> | ||
<page name='Untitled Page 2'/> | ||
</pages> | ||
</playground> |
7 changes: 7 additions & 0 deletions
7
LearnAboutAutoLayout.playground/playground.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+26.6 KB
...round/playground.xcworkspace/xcuserdata/ehyche.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+2.39 KB
(110%)
...codeproj/project.xcworkspace/xcuserdata/ehyche.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
23 changes: 23 additions & 0 deletions
23
LearnAboutStackViews/LearnAboutStackViews/Assets.xcassets/trash-can.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "trash.png", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+383 Bytes
...outStackViews/LearnAboutStackViews/Assets.xcassets/trash-can.imageset/trash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+791 Bytes
...StackViews/LearnAboutStackViews/Assets.xcassets/trash-can.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.07 KB
...StackViews/LearnAboutStackViews/Assets.xcassets/trash-can.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.