Home > Software engineering >  Can I use deprecated function in SwiftUI
Can I use deprecated function in SwiftUI

Time:11-22

I'm developing a macOS app using Xcode 14.0 with Swift 5.7 and SwiftUI. I'm running macOS 12.6 on my MacBook M1 Pro. I need to support at minimum a macOS 11.0 which means that there are many new SwiftUI functions I cannot use due to them needing macOS 12.0 . One function that I use quite a bit for SwiftUI layout is overlay(_:alignment:). According to the enter image description here

CodePudding user response:

It's safe to keep using the deprecated version of overlay. Apple won't remove it from SwiftUI for years, if ever.

But if you want to be extra safe, you can write your own modifier that uses the deprecated overlay if the new overlay is not available:

extension View {
    @ViewBuilder
    func overlayWorkaround<V: View>(
        alignment: Alignment = .center,
        @ViewBuilder _ content: () -> V
    ) -> some View {
        if #available(macOS 12, macCatalyst 15, iOS 15, tvOS 15, watchOS 8, *) {
            self.overlay(alignment: alignment, content: content)
        } else {
            self.overlay(content(), alignment: alignment)
        }
    }
}
  • Related