Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ final class CallbackButton: UIView {
let onTap: () -> ()
let button: UIButton

init(title: String, onTap: () -> ()) {
init(title: String, onTap: @escaping () -> ()) {
self.onTap = onTap
self.button = UIButton(type: .System)
self.button = UIButton(type: .system)
super.init(frame: .zero)
addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.constrainEdges(to: self)
button.setTitle(title, forState: .Normal)
button.addTarget(self, action: #selector(tapped), forControlEvents: .TouchUpInside)
button.setTitle(title, for: .normal)
button.addTarget(self, action: #selector(tapped), for: .touchUpInside)
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -53,9 +53,9 @@ extension UIStackView {
convenience init(elements: [ContentElement]) {
self.init()
translatesAutoresizingMaskIntoConstraints = false
axis = .Vertical
axis = .vertical
spacing = 10

for element in elements {
addArrangedSubview(element.view)
}
Expand All @@ -74,21 +74,21 @@ final class StackViewController: UIViewController {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()
view.backgroundColor = .white

let stack = UIStackView(elements: elements)
view.addSubview(stack)
stack.constrainEqual(.Width, to: view)
stack.constrainEqual(attribute: .width, to: view)
stack.center(in: view)
}
}


let elements: [ContentElement] = [
.image([#Image(imageLiteral: "objc-logo-white.png")#]),
.image(#imageLiteral(resourceName: "objc-logo-white.png")),
.label("To use the Swift Talk app please login as a subscriber"),
.button("Login with GitHub", {
print("Button tapped")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import UIKit
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()
view.backgroundColor = .white

let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .Vertical
stack.axis = .vertical
stack.spacing = 10
view.addSubview(stack)

stack.constrainEqual(.Width, to: view)
stack.constrainEqual(attribute: .width, to: view)
stack.center(in: view)

let image = UIImageView(image: [#Image(imageLiteral: "objc-logo-white.png")#])
let image = UIImageView(image: #imageLiteral(resourceName: "objc-logo-white.png"))
stack.addArrangedSubview(image)

let text1 = UILabel()
text1.numberOfLines = 0
text1.text = "To use the Swift Talk app please login as a subscriber"
stack.addArrangedSubview(text1)

let button = UIButton(type: .System)
button.setTitle("Login with GitHub", forState: .Normal)
let button = UIButton(type: .system)
button.setTitle("Login with GitHub", for: .normal)
stack.addArrangedSubview(button)

let text2 = UILabel()
Expand All @@ -39,5 +39,3 @@ let vc = ViewController()

vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
vc.view


Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extension ContentElement {
label.text = text
return label
case .button(let title):
let button = UIButton(type: .System)
button.setTitle(title, forState: .Normal)
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
return button
case .image(let image):
return UIImageView(image: image)
Expand All @@ -29,18 +29,18 @@ extension ContentElement {
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()
view.backgroundColor = .white

let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .Vertical
stack.axis = .vertical
stack.spacing = 10
view.addSubview(stack)

stack.constrainEqual(.Width, to: view)
stack.constrainEqual(attribute: .width, to: view)
stack.center(in: view)

let image = ContentElement.image([#Image(imageLiteral: "objc-logo-white.png")#]).view
let image = ContentElement.image(#imageLiteral(resourceName: "objc-logo-white.png")).view
stack.addArrangedSubview(image)

let text1 = ContentElement.label("To use the Swift Talk app please login as a subscriber").view
Expand All @@ -60,4 +60,3 @@ let vc = ViewController()
vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
vc.view


Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extension ContentElement {
label.text = text
return label
case .button(let title):
let button = UIButton(type: .System)
button.setTitle(title, forState: .Normal)
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
return button
case .image(let image):
return UIImageView(image: image)
Expand All @@ -30,7 +30,7 @@ extension UIStackView {
convenience init(elements: [ContentElement]) {
self.init()
translatesAutoresizingMaskIntoConstraints = false
axis = .Vertical
axis = .vertical
spacing = 10

for element in elements {
Expand All @@ -43,23 +43,23 @@ extension UIStackView {
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()

let image = ContentElement.image([#Image(imageLiteral: "objc-logo-white.png")#])
view.backgroundColor = .white
let image = ContentElement.image(#imageLiteral(resourceName: "objc-logo-white.png"))

let text1 = ContentElement.label("To use the Swift Talk app please login as a subscriber")

let button = ContentElement.button("Login with GitHub")

let text2 = ContentElement.label("If you're not registered yet, please visit http://objc.io for more information")

let stack = UIStackView(elements: [image, text1, button, text2])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .Vertical
stack.axis = .vertical
stack.spacing = 10
view.addSubview(stack)

stack.constrainEqual(.Width, to: view)
stack.constrainEqual(attribute: .width, to: view)
stack.center(in: view)
}
}
Expand All @@ -70,4 +70,3 @@ let vc = ViewController()
vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
vc.view


Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extension ContentElement {
label.text = text
return label
case .button(let title):
let button = UIButton(type: .System)
button.setTitle(title, forState: .Normal)
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
return button
case .image(let image):
return UIImageView(image: image)
Expand All @@ -30,7 +30,7 @@ extension UIStackView {
convenience init(elements: [ContentElement]) {
self.init()
translatesAutoresizingMaskIntoConstraints = false
axis = .Vertical
axis = .vertical
spacing = 10

for element in elements {
Expand All @@ -43,22 +43,22 @@ extension UIStackView {
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()
view.backgroundColor = .white

let elements: [ContentElement] = [
.image([#Image(imageLiteral: "objc-logo-white.png")#]),
.image(#imageLiteral(resourceName: "objc-logo-white.png")),
.label("To use the Swift Talk app please login as a subscriber"),
.button("Login with GitHub"),
.label("If you're not registered yet, please visit http://objc.io for more information")
]

let stack = UIStackView(elements: elements)
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .Vertical
stack.axis = .vertical
stack.spacing = 10
view.addSubview(stack)

stack.constrainEqual(.Width, to: view)
stack.constrainEqual(attribute: .width, to: view)
stack.center(in: view)
}
}
Expand All @@ -68,5 +68,3 @@ let vc = ViewController()

vc.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
vc.view


27 changes: 13 additions & 14 deletions StackViews with Enums.playground/Sources/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@ import UIKit

extension UIView {
public func constrainEqual(attribute: NSLayoutAttribute, to: AnyObject, multiplier: CGFloat = 1, constant: CGFloat = 0) {
constrainEqual(attribute, to: to, attribute, multiplier: multiplier, constant: constant)
constrainEqual(attribute: attribute, to: to, attribute, multiplier: multiplier, constant: constant)
}

public func constrainEqual(attribute: NSLayoutAttribute, to: AnyObject, _ toAttribute: NSLayoutAttribute, multiplier: CGFloat = 1, constant: CGFloat = 0) {
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: self, attribute: attribute, relatedBy: .Equal, toItem: to, attribute: toAttribute, multiplier: multiplier, constant: constant)
])
NSLayoutConstraint.activate([
NSLayoutConstraint(item: self, attribute: attribute, relatedBy: .equal, toItem: to, attribute: toAttribute, multiplier: multiplier, constant: constant)
])
}

public func constrainEdges(to view: UIView) {
constrainEqual(.Top, to: view, .Top)
constrainEqual(.Leading, to: view, .Leading)
constrainEqual(.Trailing, to: view, .Trailing)
constrainEqual(.Bottom, to: view, .Bottom)
constrainEqual(attribute: .top, to: view, .top)
constrainEqual(attribute: .leading, to: view, .leading)
constrainEqual(attribute: .trailing, to: view, .trailing)
constrainEqual(attribute: .bottom, to: view, .bottom)
}

/// If the `view` is nil, we take the superview.
public func center(in view: UIView? = nil) {
guard let container = view ?? self.superview else { fatalError() }
centerXAnchor.constrainEqual(container.centerXAnchor)
centerYAnchor.constrainEqual(container.centerYAnchor)
centerXAnchor.constrainEqual(anchor: container.centerXAnchor)
centerYAnchor.constrainEqual(anchor: container.centerYAnchor)
}
}

extension NSLayoutAnchor {
public func constrainEqual(anchor: NSLayoutAnchor, constant: CGFloat = 0) {
let constraint = constraintEqualToAnchor(anchor, constant: constant)
constraint.active = true
constraint(equalTo: anchor, constant: constant).isActive = true
}
}
}
10 changes: 1 addition & 9 deletions StackViews with Enums.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios'>
<pages>
<page name='Final Code'/>
<page name='Step 3'/>
<page name='Step 2'/>
<page name='Step 1'/>
<page name='Original'/>
</pages>
</playground>
<playground version='6.0' target-platform='ios'/>