Skip to content

Instantly share code, notes, and snippets.

View michaelevensen's full-sized avatar

Michael Nino Evensen michaelevensen

View GitHub Profile
@michaelevensen
michaelevensen / MinMax.swift
Last active March 24, 2017 09:44
min()max() Example
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Move ContainerView based on pagination from custom pagingScrollView
if scrollView == pagingScrollView {
// Calculate offset
let offset = (self.view.frame.size.height - bottomPeekMargin) - scrollView.contentOffset.y
// Overlay value (interpolates between 0.0 and 1.0 for end value)
// Note to self: contentOffset divided by endValue (where you end the animation)
@michaelevensen
michaelevensen / EmbedViewController.swift
Last active September 5, 2022 07:27
Embed a UIViewController in Container View programatically
// Instantiate
self.mapViewController = self.storyboard?.instantiateViewController(withIdentifier: "MapViewController") as? MapViewController
if let mapViewController = self.mapViewController {
// Add to Container View
self.mapViewContainerView.addSubview(mapViewController.view)
self.addChildViewController(mapViewController)
mapViewController.didMove(toParentViewController: self)
}
@michaelevensen
michaelevensen / ContainerView.swift
Created March 29, 2017 19:10
Adding / removing from a containerView
func add(asChildViewController viewController: UIViewController) {
// Add Child View Controller
self.addChildViewController(viewController)
// Add Child View as Subview
self.drawerContainerView.addSubview(viewController.view)
// self.view.addSubview(viewController.view)
// Configure Child View
@michaelevensen
michaelevensen / UITextView
Created August 2, 2017 12:45
UITextView IBDesignable for removing inner padding
@IBDesignable class UITextViewFixed: UITextView {
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
func setup() {
textContainerInset = UIEdgeInsets.zero
textContainer.lineFragmentPadding = 0
}
}
@michaelevensen
michaelevensen / Int+Random.swift
Created August 15, 2017 08:09
Random number between range.
extension Int {
static func random(range: Range<Int>) -> Int {
var offset = 0
if range.lowerBound < 0 {
offset = abs(range.lowerBound)
}
let mini = UInt32(range.lowerBound + offset)
let maxi = UInt32(range.upperBound + offset)
@michaelevensen
michaelevensen / DescendingFirebase.swift
Last active August 22, 2017 20:17
Correct Ordering Descending Firebase
/*
* Custom Ordering Field: Descending
* - Trust me, this is the simplest way to do this with Firebase. 🤘
*/
imageObj["order"] = -image.uploadedAt.timeIntervalSince1970
/* */
FirebaseDatabaseReference.photos
//
// OneLineForEachSectionLayout.swift
//
// Created by Philipp Kinschel on 28/11/2016.
//
//
//
// very basic solution to layout one section in one line without linebreaks
// TODO: find name for the class
//
@michaelevensen
michaelevensen / UITextView+Margin.swift
Created September 7, 2017 11:00
Global extension to remove all padding from all UITextView's.
/// Global extension to remove all padding from all UITextView's.
extension UITextView {
open override func setNeedsDisplay() {
super.setNeedsDisplay()
textContainerInset = UIEdgeInsets.zero
textContainer.lineFragmentPadding = 0
}
}
@michaelevensen
michaelevensen / FirebaseObservers.swift
Last active January 28, 2020 22:54
childAdded and childChanged UITableView handling with Firebase. Handles both live cell updates and new children.
// Database Reference
var ref: DatabaseReference!
var handle: UInt!
var updateHandler: UInt!
// Local store
var wishes = [Wish]()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
@michaelevensen
michaelevensen / KerningLabel.swift
Last active January 16, 2018 18:03
Adds a kerning property to UILabel.
import UIKit
@IBDesignable
class KerningLabel: UILabel {
@IBInspectable var kerning: CGFloat = 0.0 {
didSet {
if attributedText?.length == nil { return }
let attrStr = NSMutableAttributedString(attributedString: attributedText!)