- Published on
NavigationSplitViewStyle protocol in SwiftUI
- Authors
- Name
什么是NavigationSplitViewStyle
NavigationSplitViewStyle is a protocol in SwiftUI.
A type that specifies the appearance and interaction of navigation split views within a view hierarchy.
SwiftUI是如何定义的
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
public protocol NavigationSplitViewStyle {
/// A view that represents the body of a navigation split view.
associatedtype Body : View
/// Creates a view that represents the body of a navigation split view.
///
/// SwiftUI calls this method for each instance of ``NavigationSplitView``
/// where this style is the current ``NavigaitonSplitViewStyle``.
///
/// - Parameter configuration: The properties of the instance to create.
@ViewBuilder func makeBody(configuration: Self.Configuration) -> Self.Body
/// The properties of a navigation split view instances.
typealias Configuration = NavigaitonSplitViewStyleConfiguration
}
SwiftUI如何调用
首先,SwiftUI会给 View这个协议进行扩展,给其添加一个方法
extension View {
/// Sets the style for navigation split views within this view.
///
/// - Parameter style: The style to set.
///
/// - Returns: A view that uses the specified navigation split view style.
@avaiable(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
public func navigationSplitViewStyle<S>(_ style: S) -> some View where S : NavigationSplitViewStyle
}
这个方法名叫navigationSplitViewStyle ,会向其传入一个参数 style,这个style会遵循上面定义的NavigationSplitViewStyle 协议,同时SwiftUI也给我们定义了几种Style。
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
extension NavigationSplitViewStyle where Self == BalancedNavigationSplitViewStyle {
/// A navigation split style that reduces the size of the detail content
/// to make room when showing the leading column or columns.
public static var balanced: BalancedNavigationSplitViewStyle { get }
}
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
extension NavigationSplitViewStyle where Self == ProminentDetailNavigationSplitViewStyle {
/// A navigation split style that attempts to maintain the size of the
/// detail content when hiding or showing the leading columns.
public static var prominentDetail: ProminentDetailNavigationSplitViewStyle { get }
}
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
extension NavigationSplitViewStyle where Self == AutomaticNavigationSplitViewStyle {
/// A navigation split style that resolves its appearance automatically
/// based on the current context.
public static var automatic: AutomaticNavigationSplitViewStyle { get }
}