UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Sheet .presentationDetents iOS16 option compatible mode

Forums > 100 Days of SwiftUI

I'm trying to add the new option for sheet view presentation to my app:

.sheet(isPresented: $isShowingInstrumentsAdd) {
                InstrumentsMaintenanceView()
            }
            .presentationDetents([.medium, .fraction(0.7)])

Xcode says "presentationDetents' is only available in iOS 16.0 or newer", and need to add an "ifAvailable" option. But then, as .sheet is a view property, I can't do something like this:

.sheet(isPresented: $isShowingInstrumentsAdd) {
                InstrumentsMaintenanceView()
            }
            if #available(iOS 16, *) {
                .presentationDetents([.medium, .fraction(0.7)])
            }

I can't find a way to add this two-option sheet, both for iOS16 or not, without having to recreate two views, one for each case. Any ideas?

1      

No, you cannot do that.

If it is important enough to you, then you could write a feedback to Apple, and request this for some future iOS version.

In the meantime you will need to use something like this

.sheet(isPresented: $isShowingInstrumentsAdd) {
    if #available(iOS 16, *) {
        InstrumentsMaintenanceView()
            .presentationDetents([.medium, .fraction(0.7)])
    } else {
        InstrumentsMaintenanceView()
    }
}

1      

Thanks. I'll do that for Apple.

I've tried the solution you give, without success. Now it works! Maybe some syntax typo.

Great!

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.