Lewis
Lewis

Reputation: 139

Presenting two sheets in SwiftUI

I'm trying to present two sheets in SwiftUI. The first sheet (SecondScreen) opens up on the Main Page (tapping the Navigation Tool Bar Icon) and the second sheet is a ShareSheet which should pop up inside the SecondScreen as an option. I have used a Form to build the SecondScreen. In the Simulator and on my device, the ShareSheet doesn't appear. I hope this is just a bug and not something Apple doesn't allow without big UI changes.

I tried to open the ShareSheet, while having the SecondScreen as a .fullScreenCover., instead of .sheet but the button still doesn't react.

Example

import SwiftUI

struct ContentView: View {
    
    @State var showMore: Bool = false
    
    var body: some View {
        NavigationView {
            Text("Main Page")
                .padding()
                .navigationBarTitle("Main Page")
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {

                    Button(action: {
                        showMore.toggle()
                        
                    }, label: {
                        Image(systemName: "ellipsis.circle")
                    })
                    .sheet(isPresented: $showMore, content: {
                        SecondScreen()
            })
         }
    }
  }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct SecondScreen: View {
    var body: some View {
        NavigationView {
           Form {
            Section {
            Button(action: {
                    ShareID (Info: "https://www.google.com")}, label: { Text("Share")
            })
            }

            }
            }
           }
        }
    }

func ShareID(Info: String){
    
let infoU = Info
    
let av = UIActivityViewController(activityItems: [infoU], applicationActivities: nil)
    UIApplication.shared.windows.first?
    .rootViewController?.present(av, animated: true,
    completion: nil)
    
}


Thank you!

Upvotes: 0

Views: 1585

Answers (1)

this is another approach to popup your sheets, even works on my mac:

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    @State var showMore: Bool = false
    
    var body: some View {
        NavigationView {
            Text("Main Page")
                .padding()
                .navigationBarTitle("Main Page")
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: { showMore.toggle() }) {
                            Image(systemName: "ellipsis.circle")
                        }
                        .sheet(isPresented: $showMore) {
                            SecondScreen()
                        }
                    }
                }
        }
    }
}

struct SecondScreen: View {
    @State var shareIt = false
    @State var info = "https://www.google.com"
    
    var body: some View {
        Button(action: {shareIt = true}) {
            Text("Share")
        }
        .sheet(isPresented: $shareIt, onDismiss: {shareIt = false}) {
            ShareSheet(activityItems: [info as Any])
        }
    }
}

struct ShareSheet: UIViewControllerRepresentable {

    typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void
    
    let activityItems: [Any]
    let applicationActivities: [UIActivity]? = nil
    let excludedActivityTypes: [UIActivity.ActivityType]? = nil
    let callback: Callback? = nil
    
    func makeUIViewController(context: Context) -> UIActivityViewController {
        let controller = UIActivityViewController(
            activityItems: activityItems,
            applicationActivities: applicationActivities)

        controller.excludedActivityTypes = excludedActivityTypes
        controller.completionWithItemsHandler = callback
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) { }
}

Upvotes: 1

Related Questions