Skip to content

Instantly share code, notes, and snippets.

View timonus's full-sized avatar

Tim Johnsen timonus

View GitHub Profile
@timonus
timonus / programmatic-dynamic-images.m
Last active January 1, 2024 12:08
Programmatically create iOS 13 dynamic images
- (UIImage *)dynamicImage
{
UITraitCollection *const baseTraitCollection = /* an existing trait collection */;
UITraitCollection *const lightTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]]];
UITraitCollection *const purelyDarkTraitCollection = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark];
UITraitCollection *const darkTraitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[baseTraitCollection, purelyDarkTraitCollection]];
__block UIImage *lightImage;
[lightTraitCollection performAsCurrentTraitCollection:^{
lightImage = /* draw image */;
@timonus
timonus / SFSafariViewController+ContextMenuBugWorkaround.m
Last active August 20, 2020 02:33
Workaround for SFSafariViewController preview + iOS 13 Context Menu negative interaction bug (FB7564018)
// Workaround for SFSafariViewController preview + iOS 13 context menu negative interaction bug (FB7564018)
// Examples: https://db.tt/hvBJOBdZwL, https://db.tt/egp20bSfS0
// With fix: https://db.tt/r5nCLNYbfs
@interface SFSafariViewController (ContextMenuBugWorkaround)
/// If this returns @c YES it's safe to present this view controller when "committing" from an iOS 13 context menu. If not, you should create a fresh @c SFSafariViewController and present that instead.
- (BOOL)canBePresentedOnContextMenuCompletion;
@end
@timonus
timonus / TJSwizzle.m
Created March 7, 2023 16:59
Swizzling copypasta
#import <objc/runtime.h>
void tj_swizzle(Class class, SEL originalSelector, SEL swizzledSelector)
{
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
@timonus
timonus / SwiftLeakPlayground.swift
Created March 24, 2023 21:57
Swift Leak Playground
//
// ViewController.swift
// Playground
//
// Created by Tim Johnsen on 3/24/23.
//
import UIKit
class MyObject {