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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.