Skip to content

Instantly share code, notes, and snippets.

View JasonCanCode's full-sized avatar

Jason JasonCanCode

View GitHub Profile
import UIKit
/**
A convenient way to filter your table with a search bar.
Example use:
extension MyTableViewController: SearchTableType {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
@JasonCanCode
JasonCanCode / PostFixTextField.swift
Created October 8, 2018 18:32
Have trailing text persist in a text field.
import UIKit
class PostFixTextField: UITextField {
@IBInspectable var postfixColor: UIColor?
@IBInspectable var shouldRemovePostfixOnEditing: Bool = false
@IBInspectable var postfixText: String = "" {
willSet {
removePostFix()
}
didSet {
@JasonCanCode
JasonCanCode / TimestampPickerView.swift
Last active August 26, 2019 20:05
A UIPickerView subclass that behaves like a UIDatePicker but for time with seconds included. Military time is optional.
import UIKit
class TimestampPickerView: UIPickerView {
private(set) var date: Date = Date()
var minimumDate: Date?
var maximumDate: Date?
var isMilitaryTime: Bool {
get {
return timestampDatasource?.isMilitaryTime ?? false
}
@JasonCanCode
JasonCanCode / XCTestCase+Helper.swift
Last active November 13, 2018 21:16
An easier way to access UI elements or check for existence within a UI Test.
extension XCTestCase {
var app: XCUIApplication { return XCUIApplication() }
func checkExistenceOfElements(_ typesAndTexts: [(XCUIElement.ElementType, String)], timeout: TimeInterval = 3) {
for (type, text) in typesAndTexts {
checkExistenceOfElement(type, text, timeout: timeout)
}
}
func checkExistenceOfElement(_ type: XCUIElement.ElementType, _ text: String, timeout: TimeInterval = 3) {
@JasonCanCode
JasonCanCode / TextFieldsKeyboardHandler.swift
Last active August 15, 2022 17:01
A helper class for handling the keyboard for views with one or more text fields
import UIKit
class TextFieldsKeyboardHandler: NSObject, UITextFieldDelegate {
private weak var delegate: TextFieldsKeyboardHandlerDelegate!
/// Used to resolve issues with old devices having offset y origins where nav bars are present
private var restingYOrigin: CGFloat = 0
/// Replace for a different handling of the keyboard offset
lazy var updateKeyboardFrame: (CGRect) -> Void = updateViewOffset
init(delegate: TextFieldsKeyboardHandlerDelegate, shouldAddDismissGesture: Bool = true) {
@JasonCanCode
JasonCanCode / PhotoImageUpdatable.swift
Created November 28, 2018 21:53
Helper protocol for populating an image view with a taken photo or one from the library.
import AVFoundation
import MobileCoreServices
import Photos
import UIKit
enum CaptureError: Error {
case cameraUnavailable
case cameraDenied
case libraryUnavailable
}
import UIKit
extension UIImageView {
func setAnimationImagesWithGif(named name: String, animationDuration: TimeInterval? = nil) {
guard let bundleURL = Bundle.main.url(forResource: name, withExtension: "gif"),
let imageData = try? Data(contentsOf: bundleURL),
let source = CGImageSourceCreateWithData(imageData as CFData, nil) else {
return
}
let frameCount = CGImageSourceGetCount(source)
@JasonCanCode
JasonCanCode / DynamicHeaderExample.swift
Last active April 18, 2021 18:45
Dynamic Table View Header Height
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
sizeTableViewHeaderToFit()
}
// MARK: - Dynamic Header Height
func sizeTableViewHeaderToFit() {
guard let headerView = tableView.tableHeaderView else {
@JasonCanCode
JasonCanCode / UIView+Shadow.swift
Created October 17, 2019 14:57
For when you work with a designer that goes crazy with the shadows
import UIKit
extension UIView {
func addShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.15
layer.shadowOffset = CGSize(width: 0, height: 3.0)
layer.shadowRadius = 4
layer.masksToBounds = false
}
@JasonCanCode
JasonCanCode / A_LinkRouter.swift
Last active April 13, 2023 16:31
Streamlined structure for inspecting a link and informing the right coordinator to take action.
import Foundation
/// Manages all the link handlers and routes a url to the appropriate one.
public class LinkRouter: NSObject {
private(set) static var shared: LinkRouter = LinkRouter()
public var appScheme: String?
private var handlers: [LinkHandlerType] = []
public init(appScheme: String? = nil) {