How to show SwiftUI PhotosPicker programatically

Hi I would like to show the PhotosPicker programatically the same way we can do with sheet(isPresented: Binding<Bool>) and fullScreenCover(isPresented: Binding<Bool>), i.e. I would like:

photosPicker(isPresented: Binding<Bool>, selectedItem: Binding<PhotosPickerItem?>).

This would allow me to have multiple buttons that show the picker and would make all my code that shows sheets consistent, thanks for reading.

Accepted Reply

There is already a modifier for this. It has the same parameters as the PhotosPicker view and comes in its different variants.

photosPicker(isPresented:selection:maxSelectionCount:selectionBehavior:matching:preferredItemEncoding:photoLibrary:) -> some View

I couldn't find it in the documentation, but you can find it in Xcode's generated interface for SwiftUI.

  • Excellent, thanks for taking the time to let me know about this! It works exactly as I wanted but sadly the way the picker view controller is shown is still very buggy so I likely will still use my own version using UIViewControllerRepresentable. Very strange it is missing from the documentation, I'm guessing its because the doc parser is not smart enough to process an extension of View in SwiftUI framework when working on the PhotosUI framework - submitted feedback FB11434717.

  • Do you mind sharing what part of the PhotosPicker API is still buggy?

Add a Comment

Replies

There is already a modifier for this. It has the same parameters as the PhotosPicker view and comes in its different variants.

photosPicker(isPresented:selection:maxSelectionCount:selectionBehavior:matching:preferredItemEncoding:photoLibrary:) -> some View

I couldn't find it in the documentation, but you can find it in Xcode's generated interface for SwiftUI.

  • Excellent, thanks for taking the time to let me know about this! It works exactly as I wanted but sadly the way the picker view controller is shown is still very buggy so I likely will still use my own version using UIViewControllerRepresentable. Very strange it is missing from the documentation, I'm guessing its because the doc parser is not smart enough to process an extension of View in SwiftUI framework when working on the PhotosUI framework - submitted feedback FB11434717.

  • Do you mind sharing what part of the PhotosPicker API is still buggy?

Add a Comment

Might be as apple engineer you have some future versions on your desktop :-) I'm using Xcode 14.2 currently, no "isPresented" option is shown when typing PhotosPicker ... Or was it removed again?