Skip to content

Instantly share code, notes, and snippets.

View zakbarlow1995's full-sized avatar

Zak Barlow zakbarlow1995

  • UK
View GitHub Profile
@zakbarlow1995
zakbarlow1995 / EasyTransparentNavigationBar.swift
Created July 19, 2019 09:59
Helper methods for quickly setting up a fully transparent navigation bar with black/white status bar text (use in viewWillAppear)
import UIKit
extension UIViewController {
func setupTransparentNavigationBarWithBlackText() {
setupTransparentNavigationBar()
navigationController?.navigationBar.barStyle = .default
navigationController?.navigationBar.tintColor = .black
}
func setupTransparentNavigationBarWithWhiteText() {
@zakbarlow1995
zakbarlow1995 / UIColorRandom+Inverted.swift
Last active September 16, 2021 23:01
UIColor extension to add .random & .inverted static and computed variables, respectively.
import UIKit
// Extension adding class var random & computed property inverted to UIColor
extension UIColor {
static var random: UIColor {
UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: .random(in: 0...1))
}
static var randomSolid: UIColor {
UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: 1.0)
}
@zakbarlow1995
zakbarlow1995 / Regex.swift
Last active July 24, 2019 09:35
String extension to add a regex comparison operator "~="
import Foundation
extension String {
static func ~= (lhs: String, rhs: String) -> Bool {
guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
let range = NSRange(location: 0, length: lhs.utf16.count)
return regex.firstMatch(in: lhs, options: [], range: range) != nil
}
}
@zakbarlow1995
zakbarlow1995 / CGSize+CGFloatExtensions.swift
Last active July 23, 2019 09:28
Overloading Swift Operators: CGFloat * CGSize
import UIKit
func * (lhs: CGSize, rhs: CGFloat) -> CGSize {
return CGSize(width: lhs.width * rhs, height: lhs.height * rhs)
}
func * (lhs: CGFloat, rhs: CGSize) -> CGSize {
return CGSize(width: lhs * rhs.width, height: lhs * rhs.height)
}
@zakbarlow1995
zakbarlow1995 / SubviewsRecursive.swift
Created July 24, 2019 09:18
UIView extension to retrieve all subviews recursively
import UIKit
extension UIView {
var subviewsRecursive: [UIView] {
return subviews + subviews.flatMap { $0.subviewsRecursive }
}
}
@zakbarlow1995
zakbarlow1995 / AddSubviews.swift
Last active July 24, 2019 09:30
UIView extension to add views variadically to the view-hierarchy
import UIKit
extension UIView {
func addSubviews(_ views: UIView...) {
views.forEach { addSubview($0) }
}
}
@zakbarlow1995
zakbarlow1995 / UITableView+areEmptyRowsHidden.swift
Last active August 6, 2019 14:23
Added an areEmptyRowsHidden property to UITableView: if set to true, hides the empty cells at the bottom of a table view by adding a view with a height of zero as a footer.
import UIKit
extension UITableView {
@IBInspectable var areEmptyRowsHidden: Bool {
set {
tableFooterView = newValue ? UIView(frame: .zero) : nil
}
get {
return tableFooterView != nil
}
@zakbarlow1995
zakbarlow1995 / Sweepstakes.swift
Created August 27, 2019 09:20
Rugby World Cup 2019 Sweepstakes
import Foundation
class Person {
let name: String
private var teams = [Team]()
public var sortedTeams: [Team] {
return teams.sorted(by: { $0.rank < $1.rank })
}
import Foundation
class Debouncer {
/**
Create a new Debouncer instance with the provided time interval.
- parameter timeInterval: The time interval of the debounce window.
*/
init(timeInterval: TimeInterval) {
@zakbarlow1995
zakbarlow1995 / Optional-String-Nil-Coalescing-Operator.swift
Last active September 5, 2019 16:06
Handy quality of life infix operator for string interpolation of optionals
infix operator ???: NilCoalescingPrecedence
public func ??? <T>(optional: T?, defaultValue: @autoclosure () -> String) -> String {
return optional.map { String(describing: $0) } ?? defaultValue()
}