Last active
April 18, 2024 21:01
-
-
Save ryanashcraft/87bc6fd517b64d90e2a9ee6f978b163e to your computer and use it in GitHub Desktop.
iOS 16-compatible SwiftUI equivalent of cellLayoutMarginsFollowReadableWidth
This file contains hidden or 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 | |
private struct SafeAreaInsetsFollowReadableWidthModifier: ViewModifier { | |
static let readableWidth: Double = 672 | |
@ViewBuilder | |
func body(content: Content) -> some View { | |
GeometryReader { geometry in | |
let insetLength = max(0, geometry.size.width - Self.readableWidth) / 2 | |
content | |
.safeAreaInset(edge: .leading, spacing: 0) { | |
Color.clear.frame(width: insetLength) | |
} | |
.safeAreaInset(edge: .trailing, spacing: 0) { | |
Color.clear.frame(width: insetLength) | |
} | |
} | |
} | |
} | |
public extension View { | |
func safeAreaInsetsFollowReadableWidth() -> some View { | |
self.modifier(SafeAreaInsetsFollowReadableWidthModifier()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment