Skip to content

Instantly share code, notes, and snippets.

View zoejessica's full-sized avatar
🐗

Zoë Smith zoejessica

🐗
View GitHub Profile
@JohnSundell
JohnSundell / ContentViewWithCollapsableHeader.swift
Last active October 28, 2024 00:03
A content view which renders a collapsable header that adapts to the current scroll position. Based on OffsetObservingScrollView from https://swiftbysundell.com/articles/observing-swiftui-scrollview-content-offset.
import SwiftUI
/// View that observes its position within a given coordinate space,
/// and assigns that position to the specified Binding.
struct PositionObservingView<Content: View>: View {
var coordinateSpace: CoordinateSpace
@Binding var position: CGPoint
@ViewBuilder var content: () -> Content
var body: some View {
@krzyzanowskim
krzyzanowskim / StringGetSizeThatFits.swift
Last active November 12, 2023 14:51
Calculate frame of String, that fits given width
// Excerpt from https://github.com/krzyzanowskim/CoreTextWorkshop
// Licence BSD-2 clause
// Marcin Krzyzanowski [email protected]
func getSizeThatFits(_ attributedString: NSAttributedString, maxWidth: CGFloat) -> CGSize {
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let rectPath = CGRect(origin: .zero, size: CGSize(width: maxWidth, height: 50000))
let ctFrame = CTFramesetterCreateFrame(framesetter, CFRange(), CGPath(rect: rectPath, transform: nil), nil)
@TizianoCoroneo
TizianoCoroneo / gist:ce0e9cab877b9dab59c0650308fca4de
Created April 24, 2021 15:08
Cool home navbar scrolling animation in SwiftUI
import SwiftUI
struct ContentView: View {
@State var currentOffset: CGFloat = 0
@State var topMessagePadding: CGFloat = 30
@State var fadeAlpha: Double = 0
@State var navBarFadeAlpha: Double = 0
@State var imageVerticalOffset: CGFloat = 0
@mecid
mecid / PagerView.swift
Last active August 15, 2024 06:38
PagerView in SwiftUI
//
// PagerView.swift
//
// Created by Majid Jabrayilov on 12/5/19.
// Copyright © 2019 Majid Jabrayilov. All rights reserved.
//
import SwiftUI
struct PagerView<Content: View>: View {
let pageCount: Int
@AvdLee
AvdLee / DarwinNotificationCenter.swift
Last active November 1, 2024 11:56
A notification center for Darwin Notifications. MIT License applies.
//
// DarwinNotificationCenter.swift
//
// Copyright © 2017 WeTransfer. All rights reserved.
//
import Foundation
/// A Darwin notification payload. It does not contain any userInfo, a Darwin notification is purely event handling.
public struct DarwinNotification {
@PaulWoodIII
PaulWoodIII / UIView+VisualRecursiveDescription.h
Last active February 6, 2022 15:01
Provide Swift with the amazing ability to visually print a UICollectionView's VisualRecursiveDescription
//
// UIView+VisualRecursiveDescription.h
//
// Created by Paul Wood on 8/28/19.
// Copyright © 2019 Paul Wood. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@breeno
breeno / CompositionalTwoColumnWaterfall.swift
Last active October 31, 2022 12:57
Simple take on a compositional layout with 2 column variable height items waterfall
import UIKit
class ViewController: UIViewController {
enum Section {
case main
}
struct Item: Hashable {
let height: CGFloat
@sergdort
sergdort / Iff.swift
Created June 16, 2019 22:50
Iff and Some operators for #SwiftUI. Inspired on some stuff we use in [Bento](). It lets you chain modifiers instead of doing "if - else" dance 🚀
extension View {
func iff(_ condition: Bool, _ modifier: (Self) -> AnyView) -> AnyView {
if condition {
return modifier(self).eraseToAnyView()
}
return eraseToAnyView()
}
func some<Value>(_ optional: Value?, modifier: (Value, Self) -> AnyView) -> some View {
guard let value = optional else {
@iccir
iccir / darkmode.m
Last active May 6, 2021 00:47
iOS Dark Mode Toggle
/*
During Dark Mode migration for macOS, I found it helpful to have a global hotkey
which toggled between Light/Dark Mode.
This hack attempts to do something similar for iOS.
1) Add your main window in -applicationDidFinishLaunching:
2) Triple tap the window (I tend to do this near the title bar) to flip between light and dark.
*/
@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 */;