TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How to persist view size within HSplitView separator?

Forums > SwiftUI

I have a HSplitView with two views. When I want to drop a file to this view, I replace the view with a Drop file here label. If I choose to abort the drop operation, view sizes get messed up, one view takes complete priority over another. If I complete the drop, things remain okay. So, in general, how to manage this in HSplitView?

Reproducible code below. Before dropping a file, you need to move the separator to another desired position.

struct ContentView: View {
    @State private var isDragging: Bool = false

    var body: some View {
        GeometryReader { geo in
            if isDragging {
                VStack {
                    Spacer()
                    Text("Drop file here")
                    Spacer()
                }
            } else {
                HSplitView {
                    ZStack {
                        Rectangle()
                            .fill(.background)
                        Text("Some Text")
                    }
                    ZStack {
                        Rectangle()
                            .fill(.background)
                        Text("Some Other Text")
                    }
                }
            }
        }
        .padding()
        .frame(width: 500, height: 500)
        .onDrop(of: ["public.file-url"],
                delegate:
                    DropFileDelegate<Any>(isDragging: $isDragging.animation()))
    }
}

struct DropFileDelegate<Model>: DropDelegate {
    @Binding var isDragging: Bool

    func dropExited(info: DropInfo) {
        isDragging = false
    }

    func dropUpdated(info: DropInfo) -> DropProposal? {
        isDragging = true
        return nil
    }

    func validateDrop(info: DropInfo) -> Bool {
        return info.hasItemsConforming(to: ["public.file-url"])
    }

    func performDrop(info: DropInfo) -> Bool {
        return true
    }
}

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.