Skip to content

Instantly share code, notes, and snippets.

@MylesCaley
MylesCaley / LandscapeOnFullScreen.swift
Last active February 1, 2019 03:56
When you have a portrait-only app you may still want to allow rotation when the app plays full screen media. This is a decent solution by creating a notification to signal a "full screen". Only tested on iOS9.
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var isFullScreen = false
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// create notifications from wherever to signal fullscreen
@MylesCaley
MylesCaley / ExpandUITextView.swift
Last active January 24, 2017 19:57
When text is too small to fit in UITextView
extension UITextView {
func sizeThatWillFitAllText()->CGFloat {
return self.sizeThatFits((self.frame.size)).height
}
}
self.textViewHeightConstraintOutlet.constant = desiredTextViewHeight < CGFloat(75) ? desiredTextViewHeight : CGFloat(75)
let textViewWantsHeightObservable = self.textViewHeightConstraintOutlet
@MylesCaley
MylesCaley / MultipleOfSameConstraints.swift
Created November 18, 2016 17:31
its possible to create multiple constraints for the same object, then change the priority
// instead of creating and destroying constraints all the time we can just
// add multiple constraints on the same object and then change the priority
// and indicate which one is active
if feedItem!.prototypeUpdate?.media.count <= 1 {
mediaViewWidthConstraintLarge.priority = 995
mediaViewWidthContraint.priority = 999
mediaViewWidthConstraintLarge.isActive = true
mediaViewWidthContraint.isActive = false
} else {
mediaViewWidthConstraintLarge.priority = 999
@MylesCaley
MylesCaley / LoadViewFromXIB.swift
Created November 29, 2016 14:49
Load a view from an XIB
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle : Bundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiate(withOwner: nil, options: nil)[0] as? UIView
}
}
@MylesCaley
MylesCaley / DynamicRxTableHeader.swift
Created November 29, 2016 14:53
Creating a dynamic header with multiple sections and multiple items
//section model file
import RxDataSources
enum CommentTableSectionModel {
case section(comment: Comment, items: [CommentTableSectionModelItem])
}
extension CommentTableSectionModel: SectionModelType {
@MylesCaley
MylesCaley / DynamicHeaderSizeForAutolayout.swift
Last active December 29, 2021 03:30
size the top header dynamically for autolayout after values have been set
// MAKE SURE YOU HAVE TOP TO BOTTOM CONSTRAINTS SET ON ALL OF THE OBJECTS IN THE HEADER VIEW!!!
// better as an extension
func sizeHeaderToFit() {
if let tableViewHeader = self.tableHeaderView {
let headerView = tableViewHeader
headerView.setNeedsLayout()
headerView.layoutIfNeeded()
@MylesCaley
MylesCaley / weakclosure.swift
Created December 8, 2016 14:10
pattern for weak closure
{ [weak self] (_) in
guard let _ = self else { return }
self!.
}
@MylesCaley
MylesCaley / Animation Tutorial Notes
Last active December 30, 2016 16:07
notes from animation tutorials
Fixes for animation tutorial
@MylesCaley
MylesCaley / find.swift
Created January 5, 2017 18:52
find an object with certain property in swift
let index = updates.index(where: {$0.id == self.prototypeUpdate?.id})
// there are cases where you just need to update the display and not the data
// for example if you change the constants on a constraint
self!.tableView?.beginUpdates()
self!.tableView?.endUpdates()