Skip to content

Instantly share code, notes, and snippets.

View DevAndArtist's full-sized avatar
🦅
Hacking in Swift

Adrian M. DevAndArtist

🦅
Hacking in Swift
View GitHub Profile
// This code uses 3 HIDDEN SwiftUI types, there is no guarantee
// that it will pass app review, so please use it with caution.
//
// * SwiftUI._PreferenceValue<Key>
// * SwiftUI._DelayedPreferenceView<Key, Content>
// * SwiftUI._PreferenceValue<Key>
//
// Feedback ID for an official support: (FB7577482)
//
// ⚠️ WARNING:
struct PKey: PreferenceKey {
static var defaultValue: Int?
static func reduce(value: inout Int?, nextValue: () -> Int?) {
print(#function, "value:", value as Any, "nextValue:", nextValue() as Any)
value = nextValue()
}
}
struct ContentView: View {
let condition = true
// BUG: FB7569572
struct GeometrySizeKey: PreferenceKey {
static func reduce(value: inout CGSize?, nextValue: () -> CGSize?) {
value = nextValue()
}
}
extension View {
func readGeometrySize() -> some View {
import SwiftUI
class Base: CustomDebugStringConvertible {
var debugDescription: String {
"\(type(of: self)) \(ObjectIdentifier(self))"
}
init() {
print("🟢 init\t", self)
}
import Combine
import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper
public struct Model<Value>: DynamicProperty {
final class _Box: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var value: Value {
import SwiftUI
struct ContentView: View {
@State var flag = true
var body: some View {
let text = "\(flag ? "HStack" : "ZStack")"
return VStack {
Text(text)
Button("change the stack") {
@DevAndArtist
DevAndArtist / fake_animation_completion.swift
Created September 18, 2019 12:24
SwiftUI Fake Animation completion for animations that do not overshoot the final value
fileprivate struct _CompletionPreferenceKey: PreferenceKey {
typealias Value = CGFloat
static let defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
@DevAndArtist
DevAndArtist / reimplemented_parts_of_swiftui.swift
Last active May 29, 2023 14:55
This is a custom implementation of some parts of SwiftUI which I could observe.
import CoreGraphics
final class TransactionStack {
static let shared = TransactionStack()
private var _transactions: [Transaction]
private init() {
dispatchPrecondition(condition: .onQueue(.main))
_transactions = []
@DevAndArtist
DevAndArtist / swiftui_to_uikit.swift
Last active July 8, 2019 10:02
Applying SwiftUI ideas to UIKit
import RxSwift
import RxCocoa
import RxRelay
extension Disposable {
public func wrapIntoAnyDisposable() -> AnyDisposable {
return AnyDisposable(self)
}
}

WWDC 2019 Labs Questions

Size changes propagation to child view controller

In A Look Inside Presentation Controllers - WWDC 2014 session the following pattern was introduced.

  • A child view controller or a presented view controller can issue a request for size update by setting its preferredContentSize property.
  • A parent view controller or a presentation controller will receive a callback on preferredContentSizeDidChangeForChildContentContainer method.
  • At this point the receiver can decide if can allow the size request and layout the child view controller or the presented view controller.