Skip to content

Instantly share code, notes, and snippets.

@smileyborg
smileyborg / SelfSizingTableHeaderAndTableFooterViews.swift
Last active February 9, 2024 09:53
How to manually self-size UITableView tableHeaderView/tableFooterView in iOS 11
// For the best results, your tableHeaderView/tableFooterView should be a UITableViewHeaderFooterView with your content inside the contentView.
let tableHeaderView = UITableViewHeaderFooterView()
let fittingSize = CGSize(width: tableView.bounds.width - (tableView.safeAreaInsets.left + tableView.safeAreaInsets.right), height: 0)
let size = tableHeaderView.systemLayoutSizeFitting(fittingSize, withHorizontalFittingPriority: .required, verticalFittingPriority: .fittingSizeLevel)
tableHeaderView.frame = CGRect(origin: .zero, size: size)
tableView.tableHeaderView = tableHeaderView
// When you set this view to the tableHeaderView/tableFooterView on the table view, the table view will preserve the existing size of its frame.
// If you need to change the size, remove the tableHeaderView/tableFooterView, set a new frame on it, then re-set it on the table view again.
@smileyborg
smileyborg / detect_evil_merge.sh
Last active August 16, 2023 04:00
Two scripts that can be used to detect evil merges in Git. See http://stackoverflow.com/questions/27683077
#!/bin/bash
# A shell script to provide a meaningful diff output for a merge commit that can be used to determine whether the merge was evil.
# The script should be run from outside the git repository, with two arguments:
# 1 - the directory of the git repository
# 2 - the SHA for the merge commit to inspect
# The script will output one file:
# - the merge redone fresh without any conflicts resolved, diff'ed to the actual merge
output_file="diff.txt"
@smileyborg
smileyborg / SimulatedSlowLoadingImage.swift
Created August 5, 2017 02:06
Snippet showing how to create an NSItemProvider with a simulated delay for loading an image
import UIKit
import MobileCoreServices
let image = UIImage() // your actual image
let itemProvider = NSItemProvider()
itemProvider.registerDataRepresentation(forTypeIdentifier: kUTTypeJPEG as String, visibility: .all) { (completionBlock) -> Progress? in
let unitsOfWork = 10 + Int64(arc4random_uniform(UInt32(10))) // 10 - 19 units
let progress = Progress.discreteProgress(totalUnitCount: unitsOfWork)
@smileyborg
smileyborg / InteractiveTransitionCollectionViewDeselection.m
Last active January 15, 2023 13:03
Animate table & collection view deselection alongside interactive transition (for iOS 11 and later)
// UICollectionView Objective-C example
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] firstObject];
if (selectedIndexPath != nil) {
id<UIViewControllerTransitionCoordinator> coordinator = self.transitionCoordinator;
if (coordinator != nil) {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
@smileyborg
smileyborg / UIView+Orientation.h
Last active February 2, 2021 14:57
Migrating Rotation Code to iOS 8
//
// UIView+Orientation.h
//
#import <UIKit/UIKit.h>
// These macros should only be used if you MUST know the interface orientation for the device itself, for example when displaying a new UIWindow.
// This should be very rare; generally you should only look at the immediate parent view's size (or "view orientation" using the category below).
#define StatusBarOrientationIsPortrait UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])
#define StatusBarOrientationIsLandscape UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])
@smileyborg
smileyborg / Xcode7Macros.h
Last active May 26, 2020 12:08
Backwards compatible macros for Objective-C nullability annotations and generics
/**
* The following preprocessor macros can be used to adopt the new nullability annotations and generics
* features available in Xcode 7, while maintaining backwards compatibility with earlier versions of
* Xcode that do not support these features.
*/
#if __has_feature(nullability)
# define __ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
# define __ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
# define __NULLABLE nullable
@smileyborg
smileyborg / convert_imports.rb
Last active June 26, 2017 11:10
A script to convert textual imports to framework imports. Useful when converting static libraries to dynamic frameworks.
#!/usr/bin/env ruby
require 'set'
UMBRELLA_HEADER_PATH = ARGV[0] # The path to the umbrella header containing all the headers
SOURCE_ROOT_PATH = ARGV[1] # The path containing the source files to convert (will be recursively searched)
FRAMEWORK_NAME = File.basename(UMBRELLA_HEADER_PATH, ".*") # Assumes that the framework name is the same as the umbrella header filename (e.g. "MyFramework" for "MyFramework.h")
UMBRELLA_IMPORT_REGEX = /#import\s+<#{FRAMEWORK_NAME}\/.+\.h>/ # Matches "#import <FrameworkName/Header.h>"
FRAMEWORK_HEADER_REGEX = /(?<=<#{FRAMEWORK_NAME}\/).+\.h(?=>)/ # Matches "Header.h" in "<FrameworkName/Header.h>"
@smileyborg
smileyborg / keybase.md
Created September 2, 2016 03:50
Keybase

Keybase proof

I hereby claim:

  • I am smileyborg on github.
  • I am smileyborg (https://keybase.io/smileyborg) on keybase.
  • I have a public key whose fingerprint is C460 F9E5 7D47 4E26 E24A 0F4A 7FCD A6ED 6139 A71A

To claim this, I am signing this object:

@smileyborg
smileyborg / RJTableViewCell.m
Created December 3, 2014 17:28
mluisbrown/TableViewCellWithAutoLayout
/* Note that I have uncommented Line 9 to prevent constraint exceptions before the cells actually layout at their correct size. */
- (void)updateConstraints
{
if (!self.didSetupConstraints) {
// Note: if the constraints you add below require a larger cell size than the current size (which is likely to be the default size {320, 44}), you'll get an exception.
// As a fix, you can temporarily increase the size of the cell's contentView so that this does not occur using code similar to the line below.
// See here for further discussion: https://github.com/Alex311/TableCellWithAutoLayout/commit/bde387b27e33605eeac3465475d2f2ff9775f163#commitcomment-4633188
self.contentView.bounds = CGRectMake(0.0f, 0.0f, 99999.0f, 99999.0f);
@smileyborg
smileyborg / Example.swift
Last active August 29, 2015 14:06
PureLayout in Swift
func setupConstraints()
{
let views: NSArray = [view1, view2, view3]
views.autoDistributeViewsAlongAxis(.Horizontal, withFixedSpacing: 10.0, insetSpacing: true, matchedSizes: false, alignment: .AlignAllTop)
// if you don't like creating an intermediate constant, the following works using Xcode 6.1 GM:
[view1, view2, view3].autoDistributeViewsAlongAxis(.Horizontal, withFixedSpacing: 10.0, insetSpacing: true, matchedSizes: false, alignment: .AlignAllTop)
}