Handling cancel button in SwiftUI PhotoPicker (inline)

Using the new inline PhotosPicker style in iOS 17, it isn't made clear how to handle the cancel button's input, and i cannot seem to find an answer in the documentation.

PhotosPicker(
    "Select picture",
    selection: $selected,
    selectionBehavior: .default,
    photoLibrary: .shared()
)
.photosPickerStyle(.inline)

Does anybody have a solution or is this a bug that needs to be fixed?

Accepted Reply

If you don't need the "Cancel" button, you can hide it using .photosPickerDisabledCapabilities(.selectionActions).

If you want to dismiss the picker when "Cancel" button is tapped, you can observe changes to $selected binding and check if it is empty.

Usually, .inline style is used with .continuous selection behavior, .photosPickerDisabledCapabilities(.selectionActions) and .photosPickerAccessoryVisibility(.hidden).

Replies

If you don't need the "Cancel" button, you can hide it using .photosPickerDisabledCapabilities(.selectionActions).

If you want to dismiss the picker when "Cancel" button is tapped, you can observe changes to $selected binding and check if it is empty.

Usually, .inline style is used with .continuous selection behavior, .photosPickerDisabledCapabilities(.selectionActions) and .photosPickerAccessoryVisibility(.hidden).

I'm also having this issue. In the app I'm working on, the PhotosPicker is contained .inline in a thin View wrapper without any additional UI and the wrapper View is presented as a sheet. I would really appreciate to have the default Cancel button working with our .sheet presentation. But it's not. Any hints how to do that?

If you want to dismiss the picker when "Cancel" button is tapped, you can observe changes to $selected binding and check if it is empty.

Observing the changes like below doesn't get triggered when Cancel is tapped:

PhotosPicker(
  selection: $viewModel.selection,
  maxSelectionCount: 1,
  selectionBehavior: .default,
  matching: .images,
  preferredItemEncoding: .current,
  photoLibrary: .shared()
) {
  Text("Select Photos")
}
.photosPickerStyle(.inline)
.photosPickerAccessoryVisibility(.hidden, edges: .bottom)
.onChange(of: viewModel.selection, { oldValue, newValue in
    print(oldValue)
    print(newValue)
})