Skip to content

Instantly share code, notes, and snippets.

View michaelevensen's full-sized avatar

Michael Nino Evensen michaelevensen

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / HiddenPropertyControls.tsx
Created September 27, 2018 07:17
Conditionally show property controls based on props (or other values) in Framer X, returns `true` or `false`.
static propertyControls: PropertyControls = {
status: {
type: ControlType.Enum,
options: ["idle", "fetching", "success", "error"],
optionTitles: ["Idle", "Fetching", "Success", "Error"],
title: "Status"
},
errorText: {
type: ControlType.String,
title: "Error Text",
@michaelevensen
michaelevensen / DrawLabel.tsx
Created November 13, 2018 13:27
Draw Canvas Label
drawLabel(angle) {
var canvas: any = document.getElementById("canvas"),
ctx: CanvasRenderingContext2D = canvas.getContext('2d');
// Clear canvas first
ctx.clearRect(0, 0, canvas.width, canvas.height);
canvas.width = 500;
// Draw text
ctx.font = "20px Helvetica";