6

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...

5
  • i am trying to achieve the same thing in UIKit, but with no luck so far. Should go with the custom implementation
    – Stoyan
    Commented Mar 31, 2021 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
    Commented Apr 1, 2021 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
    Commented Apr 1, 2021 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
    Commented Apr 3, 2021 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: hackingwithswift.com/forums/swiftui/… Commented Jul 23, 2021 at 22:13

1 Answer 1

0

You can't entirely remove the UINavigationBar for the picker but you can remove the search bar and some other elements using:

var config = PHPickerConfiguration(photoLibrary: .shared());    
if #available(iOS 17.0, *) {
    config.disabledCapabilities = [.search, .stagingArea, .collectionNavigation, .selectionActions];
}
let imagePicker = PHPickerViewController(configuration: config);

Edit: there is also ".photosPickerAccessoryVisibility" but it is only available in SwiftUI not UIKit. Docs are here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.