Skip to content

Instantly share code, notes, and snippets.

View michaelevensen's full-sized avatar

Michael Nino Evensen michaelevensen

View GitHub Profile
@michaelevensen
michaelevensen / NativeComponentEmbed.tsx
Created September 26, 2018 09:10
An example of embedding native Framer X components into custom components.
// Set default properties
static defaultProps = {
numberOfItems: 1,
...Stack.defaultProps
}
// Items shown in property panel
static propertyControls: PropertyControls = {
numberOfItems: {
type: ControlType.Number,
@michaelevensen
michaelevensen / KeyboardBoundView.swift
Created September 6, 2018 12:35
Binds the specified UIView so that the UIVIew slides up and down based on the keyboards visibility. Do a `self.view.bindToKeyboard()` in `viewDidLoad` and you should be good.
extension UIView {
func bindToKeyboard() {
NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillChange(_ notification: NSNotification) {
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
@michaelevensen
michaelevensen / FirestoreMirror.swift
Last active August 26, 2018 16:13
Firestore Mirror Database
// These methods removes the necessity of having to clear the array for each run as it just mirrors whatever the document data is.
self.array = snapshot!.documents.flatMap({Model(dictionary: $0.data())})
DispatchQueue.main.async {
self.tableView.reloadData()
}
// Or this is even better...
// Only check for document changes
snap.documentChanges.forEach {
diff in
@michaelevensen
michaelevensen / OverlayPresentationController.swift
Created June 27, 2018 12:48
Really nice custom overlay segue that presents itself as a bottom bar where it's height is defined by the preferred content size set for the View Controller in Storyboards.
//
// OverlayPresentationController.swift
// AllIWantFor
//
// Created by Michael Nino Evensen on 13/09/2017.
// Copyright © 2017 Michael Evensen. All rights reserved.
//
import UIKit
@michaelevensen
michaelevensen / UITabBar+Height.swift
Created February 2, 2018 11:40
Custom Height for UITabBar.
class CustomTabBar: UITabBar {
@IBInspectable var height: CGFloat = 0.0
override func sizeThatFits(_ size: CGSize) -> CGSize {
var sizeThatFits = super.sizeThatFits(size)
if height > 0.0 {
sizeThatFits.height = height
}
return sizeThatFits
}
@michaelevensen
michaelevensen / Date+StartOfWeekEndOfWeek.swift
Created January 26, 2018 10:12
Start and end of week with Date.
extension Date {
var startOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
return gregorian.date(byAdding: .day, value: 1, to: sunday)
}
var endOfWeek: Date? {
let gregorian = Calendar(identifier: .gregorian)
guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
@michaelevensen
michaelevensen / DesignableLabel.swift
Last active May 30, 2018 12:15
Adds Designable UILabel `Tracking` and `Line Height` for UILabel.
@IBDesignable
class DesignableLabel: UILabel {
// Tracking
@IBInspectable var tracking: CGFloat = 0.0 {
didSet {
if attributedText?.length == nil { return }
let attrStr = NSMutableAttributedString(attributedString: attributedText!)
let range = NSMakeRange(0, attributedText!.length)
@michaelevensen
michaelevensen / NavigationBar.swift
Last active June 14, 2024 06:36
Globally remove bottom shadow for UINavigationBar and add Custom Back Button Image.
// Good place to place this is AppDelegate.didFinishLaunchingWithOptions()
// Remove Bottom Shadow
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
// Custom Back Button Image
let backButtonImage = UIImage(named: "back_button")
UINavigationBar.appearance().backIndicatorImage = backButtonImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backButtonImage
@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!)
@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)