I want to display a PhotosPicker in "half size " with the .presentationDetents([.medium, .large]) approach, but I cannot make it work. Here is a code example. If you run the example you will see that the bottom button opens a sheet in half screen. The top button will open the PhotosPicker, but not in "half size". Any help or info is greatly appreciated.
import SwiftUI
import PhotosUI
struct ContentView: View {
@State private var showingGreeting = false
@State private var selectedItem: PhotosPickerItem? = nil
@State private var selectedImageData: Data? = nil
var body: some View {
VStack {
PhotosPicker(
selection: $selectedItem,
matching: .images,
photoLibrary: .shared()) {
Text("Open PhotosPicker")
.presentationDetents([.medium, .large])
}
.onChange(of: selectedItem) { newItem in
Task {
// Retrieve selected asset in the form of Data
if let data = try? await newItem?.loadTransferable(type: Data.self)
{
selectedImageData = data
}
}
}
if let selectedImageData,
let uiImage = UIImage(data: selectedImageData) {
Image(uiImage: uiImage)
.resizable()
.scaledToFit()
.frame(width: 250, height: 250)
}
Divider()
Button("Open sheet in half screen") {
showingGreeting.toggle()
}
.sheet(isPresented: $showingGreeting) {
Text("Hello there!")
.presentationDetents([.medium, .large])
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Not currently supported in the iOS 16 SDK. Please file an enhancement request if possible! As a workaround, you can presenting a half height picker by wrapping PHPickerViewController in SwiftUI.