Skip to content

Instantly share code, notes, and snippets.

@chockenberry
Created December 19, 2024 23:06
Swipe actions breaking ShareLink()
//
// TestApp.swift
// TestApp
//
// Created by Craig Hockenberry on 12/19/24.
//
import SwiftUI
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
WTFView()
}
}
}
struct WTFView: View {
@State private var WTF = false
var body: some View {
Button("WTF!") {
WTF.toggle()
}
.sheet(isPresented: $WTF) {
// Tapping on the share icon produces something like this in the console:
// Attempt to present <UIActivityViewController: ..> on
// <_SwiftUI-UIHostingController-ModifiedContent-AnyView-RootModifier__: ..>
// (from <_SwiftUI-UIHostingController-ModifiedContent-AnyView-RootModifier__: ..>)
// which is already presenting <_SwiftUI-PresentationHostingController-AnyView_: ..>.
ShareLink(item: URL(string: "https://iconfactory.com")!) {
Label("Share", systemImage: "square.and.arrow.up")
.labelStyle(.iconOnly)
}
}
#if true // turn this off and ShareLink works fine
.swipeActions(edge: .leading) {
Button("OH RLY?") {
print("really.")
}
}
#endif
}
}
@wearhere
Copy link

Thank you for the report! You can work around by defining .swipeActions above the sheet:

.swipeActions(...)
.sheet(...)

@chockenberry
Copy link
Author

Thanks for the tip, Jeff!

For others who stumble across this, the swipe actions can be attached to the first view of a complex hierarchy of VStack/HStack. In our case, the Button above was an HStack with an avatar Image and some Text (and the text was what we wanted to share). We put the .swipeActions() on the Image.

As long as they're before the .sheet() or .fullScreenCover() it will work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment