Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open PhotosPicker from confirmationDialog in SwiftUI?

Tags:

swiftui

I would like to make an ActionSheet, where user could choose if he want to select image from Gallery or take a picture.

However, implementing this I was not able to find a way how PhotosPicker could be called in such context. The code below doesn't work: the item "Choose from gallery" displayed, but no reaction on tapping.

I will highly appreciate any advises.

Thank you!

            Button {
                displayImageActionSheet = true
            } label: {
                Image("user_photo_placeholder")
            }
            .confirmationDialog("Profile picture",
                                isPresented: $displayImageActionSheet,
                                titleVisibility: .visible) {
               
                
                PhotosPicker("Choose from gallery",
                                         selection: $selectedPhotoImage,
                                         matching: .any(of: [.videos, .not(.images)]))
                
                Button("Take picture") {
                    
                }
                
                Button("Remove", role: .destructive) {
                    
                }
            }
like image 477
Andrey Avatar asked Sep 07 '25 03:09

Andrey


1 Answers

You can present it by using photosPicker(isPresented:)

import SwiftUI
import PhotosUI
struct ConfimShowPhotos: View {
    @State var showPicker: Bool = false
    @State var showDialog: Bool = false
    @State var showCamera: Bool = false
    @State var selectedItem: PhotosPickerItem? = nil
    var body: some View {
        Button {
            showDialog.toggle()
        } label: {
            Image(systemName: "photo")
        }
        .confirmationDialog("Profile Picture", isPresented: $showDialog) {
            Button {
                showCamera.toggle()
            } label: {
                Label("Camera", systemImage: "camera")
            }
            Button {
                showPicker.toggle()
            } label: {
                Label("Gallery", systemImage: "photo.artframe")
            }
        }
        .photosPicker(isPresented: $showPicker, selection: $selectedItem)
        .alert("Say Cheese!!", isPresented: $showCamera) {
            Text("Mimicking showing camera")
        }
    }
}
like image 87
lorem ipsum Avatar answered Sep 11 '25 00:09

lorem ipsum