GeometryReader in SwiftUI: An Annoying Nightmare

แชร์
ฝัง

ความคิดเห็น • 5

  • @rebeloper
    @rebeloper  7 หลายเดือนก่อน +4

    🤦‍♂ Forgot to move the proxy out. Here's the updated code (see the GeometryProxy added):
    import SwiftUI
    struct ContentView: View {
    var body: some View {
    GeometryStack(.top(spacing: 50)) { proxy in
    Color.teal
    Text("GeometryStack: \(proxy.size)")
    }
    }
    }
    #Preview {
    ContentView()
    }
    struct GeometryStack: View {
    private var axis: GeometryStackAxis
    @ViewBuilder private var content: (GeometryProxy) -> Content
    private var verticalAlignment: VerticalAlignment
    private var horizontalAlignment: HorizontalAlignment
    private var spacing: CGFloat?
    init(_ alignment: GeometryStackAlignment = .verticalCentered(spacing: nil),
    @ViewBuilder content: @escaping (GeometryProxy) -> Content) {

    switch alignment {
    case .leading(let spacing):
    self.horizontalAlignment = .leading
    self.verticalAlignment = .center
    self.spacing = spacing
    self.axis = .vertical
    case .trailing(let spacing):
    self.horizontalAlignment = .trailing
    self.verticalAlignment = .center
    self.spacing = spacing
    self.axis = .vertical
    case .verticalCentered(let spacing):
    self.horizontalAlignment = .center
    self.verticalAlignment = .center
    self.spacing = spacing
    self.axis = .vertical
    case .top(let spacing):
    self.horizontalAlignment = .center
    self.verticalAlignment = .top
    self.spacing = spacing
    self.axis = .horizontal
    case .bottom(let spacing):
    self.horizontalAlignment = .center
    self.verticalAlignment = .bottom
    self.spacing = spacing
    self.axis = .horizontal
    case .horizontalCentered(let spacing):
    self.horizontalAlignment = .center
    self.verticalAlignment = .center
    self.spacing = spacing
    self.axis = .horizontal
    case .depth:
    self.horizontalAlignment = .center
    self.verticalAlignment = .center
    self.axis = .depth
    }
    self.content = content
    }
    var body: some View {
    GeometryReader { proxy in
    Group {
    switch axis {
    case .vertical:
    VStack(alignment: horizontalAlignment, spacing: spacing) {
    content(proxy)
    }
    case .horizontal:
    HStack(alignment: verticalAlignment, spacing: spacing) {
    content(proxy)
    }
    case .depth:
    ZStack {
    content(proxy)
    }
    }
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
    }
    enum GeometryStackAxis {
    case vertical, horizontal, depth
    }
    enum GeometryStackAlignment {
    case leading(spacing: CGFloat?), trailing(spacing: CGFloat?), verticalCentered(spacing: CGFloat?)
    case top(spacing: CGFloat?), bottom(spacing: CGFloat?), horizontalCentered(spacing: CGFloat?)
    case depth
    }
    }

  • @rebeloper
    @rebeloper  7 หลายเดือนก่อน +2

    STOP using NavigationStack in SwiftUI - Navigation Coordinator is BETTER 👉 th-cam.com/video/z5IBKIGIIzA/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @superNovaThere
    @superNovaThere 6 หลายเดือนก่อน

    Complete wate of time.

    • @rebeloper
      @rebeloper  6 หลายเดือนก่อน

      :(

    • @danielcrompton7818
      @danielcrompton7818 2 หลายเดือนก่อน

      Why is making life easier for you a waste of time? I completely disagree :