Discussion
議論
You receive a UserInterfaceSizeClass
value when you read this environment value. The value tells you about the amount of horizontal space available to the view that reads it. You can read this size class like any other of the EnvironmentValues
, by creating a property with the Environment
property wrapper:
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
SwiftUI sets this size class based on several factors, including:
Several built-in views change their behavior based on this size class. For example, a NavigationView
presents a multicolumn view when the horizontal size class is UserInterfaceSizeClass.regular
, but a single column otherwise. You can also adjust the appearance of custom views by reading the size class and conditioning your views. If you do, be prepared to handle size class changes while your app runs, because factors like device orientation can change at runtime.