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
import SwiftUI
// T.Type -> AlignmentID.Type -> Any.Type -> UInt
extension HorizontalAlignment {
enum TestH: AlignmentID {
static func defaultValue(in context: ViewDimensions) -> CGFloat {
0
}
}
import PlaygroundSupport
import Dispatch
let page = PlaygroundPage.current
page.needsIndefiniteExecution = true
struct Weak<T> {
struct _ObjectBox {
weak var object: AnyObject?
}
// BUG: FB7635852
import SwiftUI
struct ContentView: View {
let count: Int
var body: some View {
HStack
.init(alignment: .top, spacing: 0) {
// SOME OBSERVATIONS:
// * In Xcode 11.3.1 `V_Struct_Representable_ObservedObject` won't update
// which is clearly a bug. This bug is fixed in Xcode 11.4 beta 1.
//
// * `V_Struct` and `V_Struct_Representable` both never update because
// they shouldn't as the framework internal raw-diffing likely won't
// identify any change. (EXPECTED BEHAVIOR)
//
// * `V_Class` and `V_Class_ObservedObject` both produce a runtime crash,
// which might be a bug as well. `View` conforming types shouldn't be
// 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:
// BUG: FB7569572
struct GeometrySizeKey: PreferenceKey {
static func reduce(value: inout CGSize?, nextValue: () -> CGSize?) {
value = nextValue()
}
}
extension View {
func readGeometrySize() -> some View {
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
import SwiftUI
class Base: CustomDebugStringConvertible {
var debugDescription: String {
"\(type(of: self)) \(ObjectIdentifier(self))"
}
init() {
print("🟢 init\t", 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.
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") {