Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidsteppenbeck/3c9a731688e5774946db9d5221174ba9 to your computer and use it in GitHub Desktop.
Save davidsteppenbeck/3c9a731688e5774946db9d5221174ba9 to your computer and use it in GitHub Desktop.
A SwiftUI view modifier to limit the content size category of a view to a specified maximum size.
import SwiftUI
fileprivate struct ContentSizeCategoryModifier: ViewModifier {
// MARK:- Properties
/// The preferred size of the content.
@Environment(\.sizeCategory) private var sizeCategory
/// The maximum permitted content size category.
let sizeCategoryLimit: ContentSizeCategory
// MARK:- Methods
func body(content: Content) -> some View {
content.environment(\.sizeCategory, sizeCategory > sizeCategoryLimit ? sizeCategoryLimit : sizeCategory)
}
}
extension View {
/// Limits the content size category of the view to the specified value.
///
/// - Parameters:
/// - sizeCategoryLimit: The maximum permitted content size category.
func contentSizeCategoryLimited(to sizeCategoryLimit: ContentSizeCategory) -> some View {
modifier(ContentSizeCategoryModifier(sizeCategoryLimit: sizeCategoryLimit))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment