Skip to content

Instantly share code, notes, and snippets.

View stephen-francis's full-sized avatar

Stephen Francis stephen-francis

  • PillPack
  • Somerville, MA
View GitHub Profile
@stephen-francis
stephen-francis / adjustsImageSizeForAccessibilityContentSizeCategory.swift
Created March 6, 2019 00:01
adjustsImageSizeForAccessibilityContentSizeCategory
extension UIImageView {
static func scaledImageWithName(_ name: String, sizeValue: CGFloat) -> UIImageView {
let widthAndHeight = UIFontMetrics.default.scaledValue(for: sizeValue)
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: widthAndHeight, height: widthAndHeight))
imageView.image = UIImage(named: name)
return imageView
}
}
// Enable dragging from the image view (see ViewController+Drag.swift).
let dragInteraction = UIDragInteraction(delegate: self)
dragInteraction.isEnabled = true // this must be enabled for apps targeting iPhone
imageView.addInteraction(dragInteraction)
@stephen-francis
stephen-francis / genericLinkedList.swift
Last active May 8, 2017 19:33
Simple linked list in Swift
class ListNode<T> {
var data: T
var next: ListNode?
init(_ data: T) {
self.data = data
}
// nodes are inserted at the end of the list
import AudioToolbox.AudioServices
func vibrate() {
AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate) {
// do what you'd like now that the sound has completed playing
}
}
public enum OnboardingState: Int {
case notStarted
case addLocations
case addStops
case preferences
case finished
func storyboardIdentifier() -> String {
switch self {
var foodStuffs = [Food]() {
didSet {
// because we perform this operation on the main thread, it is safe
OperationQueue.main.addOperation { // this will not crash
self.tableView.reloadData()
}
}
}
class FoodTableViewController: UIViewController {
var foodStuffs = [Food]() {
didSet {
// looks good, right?
self.tableView.reloadData() // this will crash :( :(
}
}
func update() {
Updater.doSomethingInBackground { foodResults in
@stephen-francis
stephen-francis / WeatherController.swift
Created February 21, 2017 13:06
Example of getting weather info from the OpenWeatherMap API, written in Swift.
//
// WeatherController.swift
// Commute
//
// Created by Stephen Francis on 2/17/17.
// Copyright © 2017 Stephen Francis. All rights reserved.
//
import CoreLocation
// pre-iOS 9.0, UIViewControllers could update status bar hidden state at the application level, like so:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// set the status bar hidden at the application level
// status bar will be hidden on all view controllers
UIApplication.shared.isStatusBarHidden = true
}
// override this in your UIViewController subclass, dictates status bar state for the view controller
override var prefersStatusBarHidden: Bool {
return true
}