Skip to content

Instantly share code, notes, and snippets.

View pedrommcarrasco's full-sized avatar

Pedro Carrasco pedrommcarrasco

View GitHub Profile
@pedrommcarrasco
pedrommcarrasco / i-want-to-be-discardable.swift
Last active January 6, 2019 19:36
I Want To Be Discardable - Example
@discardableResult
func example() -> String {
let description = "This is an example"
print(description)
return description
}
@pedrommcarrasco
pedrommcarrasco / i-want-to-be-discardable.swift
Created January 6, 2019 19:37
I Want To Be Discardable - Before
func example() -> String {
let description = "This is an example"
print(description)
return description
}
@pedrommcarrasco
pedrommcarrasco / i-want-to-be-discardable.swift
Created January 6, 2019 19:38
I Want To Be Discardable - Usage
var value = example()
value.append(" with discardable")
print(value)
@pedrommcarrasco
pedrommcarrasco / i-want-to-be-discardable.swift
Created January 6, 2019 19:39
I Want To Be Discardable - Real World Example
let a = UIView()
let b = UIView()
a.topAnchor.constraint(equalTo: b.topAnchor)
let aBottom = a.bottomAnchor.constraint(equalTo: b.bottomAnchor)
aBottom.priority = .defaultHigh
@pedrommcarrasco
pedrommcarrasco / guide-your-layout.swift
Created January 6, 2019 20:08
Guide Your Layout - Avoid
let containerView = UIView()
view.addSubview(containerView)
@pedrommcarrasco
pedrommcarrasco / guide-your-layout.swift
Created January 6, 2019 20:09
Guide Your Layout - Add to Hierarchy
let layoutGuide = UILayoutGuide()
view.addLayoutGuide(layoutGuide)
@pedrommcarrasco
pedrommcarrasco / guide-your-layout.swift
Created January 6, 2019 20:10
Guide Your Layout - Subviews
[photoImageView, introLabel].forEach { self.view.addSubview($0) }
@pedrommcarrasco
pedrommcarrasco / guide-your-layout.swift
Created January 6, 2019 20:10
Guide Your Layout - Constraints
NSLayoutConstraint.activate(
[
layoutGuide.centerYAnchor.constraint(equalTo: view.centerYAnchor),
layoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
layoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),
photoImageView.topAnchor.constraint(equalTo: layoutGuide.topAnchor),
photoImageView.centerXAnchor.constraint(equalTo: layoutGuide.centerXAnchor),
photoImageView.widthAnchor.constraint(equalTo: photoImageView.heightAnchor),
photoImageView.heightAnchor.constraint(equalToConstant: 250),
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:17
Power-Up Your Anchors - Before
// Subviews
let logoImageView = UIImageView()
let welcomeLabel = UILabel()
let dismissButton = UIButton()
// Add Subviews & Set view's translatesAutoresizingMaskIntoConstraints to false
[logoImageView, welcomeLabel, dismissButton].forEach {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
@pedrommcarrasco
pedrommcarrasco / power-up-your-anchors.swift
Created January 6, 2019 20:18
Power-Up Your Anchors - Adding Subviews
extension UIView {
func addSubviewsUsingAutoLayout(_ views: UIView ...) {
views.forEach {
self.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
}
}