Last active
September 12, 2024 09:07
-
-
Save lukaskubanek/21913ee2c6a0a1dec829ba49fb6f345a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
extension View { | |
/// A custom popover modifier that ensures the system automatically selects the best | |
/// orientation for the popover's arrow, compatible with both iOS 17 and 18. | |
/// | |
/// On iOS 17, the `arrowEdge` is treated as a hint and may be ignored if the popover does | |
/// not fit. On iOS 18, the `arrowEdge` is strictly enforced, potentially causing layout | |
/// issues if the content doesn't fit. | |
/// | |
/// This modifier uses the new iOS 18 behavior where the system automatically selects the | |
/// best edge. Use this if you want automatic arrow orientation on iOS 18 and backward | |
/// compatibility with iOS 17. | |
/// | |
/// - Parameters: | |
/// - isPresented: A binding to a Boolean that controls the popover's visibility. | |
/// - attachmentAnchor: The attachment anchor for the popover's position. | |
/// - content: The content of the popover. | |
/// | |
/// Source: https://developer.apple.com/forums/thread/756873?answerId=802811022#802811022 | |
@ViewBuilder | |
internal func autoEdgePopover<Content: View>( | |
isPresented: Binding<Bool>, | |
attachmentAnchor: PopoverAttachmentAnchor = .rect(.bounds), | |
@ViewBuilder content: @escaping () -> Content | |
) -> some View { | |
if #available(iOS 18, *) { | |
self | |
.popover( | |
isPresented: isPresented, | |
attachmentAnchor: attachmentAnchor, | |
content: content | |
) | |
} else { | |
self | |
.popover( | |
isPresented: isPresented, | |
attachmentAnchor: attachmentAnchor, | |
arrowEdge: .top, | |
content: content | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment