5

I've been trying to implement a photo selection feature in a new app. My current approach is to use a PHPickerViewController embedded in a UIViewControllerRepresentable to display in a swiftUI view.

This is my makeUIViewController function.

func makeUIViewController(context: Context) -> PHPickerViewController {
    var configuration = PHPickerConfiguration()
    configuration.filter = filter
    configuration.selectionLimit = limit
    let controller = PHPickerViewController(configuration: configuration)
    controller.delegate = context.coordinator

    return controller
}

It is inside a struct named PhotoPicker :

struct PhotoPicker: UIViewControllerRepresentable {

What I want to hide is this part : enter image description here

Yes, all of that. Let me explain myself, the PickerView is always presented, it is not a pop-up, so there is no need for a cancel button. As you can see there is no done button either. That's because only one image needs to be selected so what happens is when the user taps on an image, the event that a new image was selected is called immediately. Removing the need for user confirmation. Then concerning the search bar, I don't really want it, I just want the user to select a photo and finally the little switch between photos and albums isn't really necessary in my case either.

I've tried a lot of different ways, including trying to set options for the controller when it is created in makeUIViewController. These options were for example :

controller.navigationController?.setToolbarHidden(true, animated: false)
controller.navigationController?.setNavigationBarHidden(true, animated: false)

And I also tried invoking view modifier in my SwiftUI body :

PhotoPicker(filter: .images, limit: 1) { (results) in
    
}
.navigationBarTitle("")
.navigationBarHidden(true)
.statusBar(hidden: true)
.navigationBarBackButtonHidden(true)

But again, none of them seems to work. So that's why I'm asking here, because it seems I tried everything and nothing is working...

Titouan
  • 531
  • 4
  • 13
  • i am trying to achieve the same thing in UIKit, but with no luck so far. Should go with the custom implementation – Stoyan Mar 31 '21 at 19:16
  • You could totally use a custom implementation but it would be a trade off for performances (as the default one is very well optimized). Plus it would be broken more easily with API changes. – Titouan Apr 01 '21 at 14:22
  • We had a custom implementation, but now we are having issues with iOS 14's ability to share only specific photos in the app, our custom controller does not work with the limited selection, so i was thinking of giving this one a try but would not happen – Stoyan Apr 01 '21 at 14:45
  • Well we seem to be having the same problem, if you could vote up the question I suppose it would get an answer faster... – Titouan Apr 03 '21 at 21:21
  • Any update about this? In my case I am getting and error on some iphones 12, where the search input if closing the sheet. Is the same error described in this link: https://www.hackingwithswift.com/forums/swiftui/uiimagepicker-sheet-disappears-when-search-field-takes-focus/6465 – Saul Burgos Jul 23 '21 at 22:13

0 Answers0