You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For common master/detail setups. A SwiftUI NavigationView really is a UI/NSSplitView already :-) But the semantics wrt to show and showDetail would be different.
Implementation shouldn't be too hard, depending on how many features are to be replicated.
A first attempt, to be finished:
/**
* Type erased version of the ``SplitViewController``. Check that for more
* information.
*/
publicprotocol_SplitViewController:_ViewController{typealiasStyle=SplitViewControllerStyletypealiasColumn=SplitViewControllerColumn}publicenumSplitViewControllerStyle:Equatable{case doubleColumn
case tripleColumn
}publicenumSplitViewControllerColumn:Equatable{case primary
case supplementary
case secondary
}
/**
* A simple wrapper around SwiftUI's `NavigationView`.
*
* Should be used as a root only.
*
* This adds a few `UISplitViewController` like behaviour, but in the end just
* hooks into `NavigationView`
* (which is a SplitViewController in wider layouts).
*
* Unlike `UISplitViewController`, this does not wrap the children in
* `NavigationController`s (this is handled by SwiftUI itself).
*
* Example:
* ```swift
* struct ContentView: View { // the "scene view"
*
* var body: some View {
* MainViewController(SplitViewController(style: .doubleColumn))
* }
* }
* ```
*
* Note that this works quite differently to a `UISplitViewController`.
*
* 2022-04-25: Note that programmatic navigation in SwiftUI is still a mess,
* i.e. popping in a 3-pane controller may fail.
*/
openclassSplitViewController:ViewController,_SplitViewController{
// TBD: We could probably make this more typesafe if we tie it to three
// columns?
@Publishedpublicvarstyle:SplitViewControllerStyle@PublishedpublicvarviewControllers:[AnyViewController]init(style:SplitViewControllerStyle=.doubleColumn,
viewControllers:[AnyViewController]=[]){self.style = style
self.viewControllers = viewControllers
}conveniencepublicinit<PrimaryVC, SupplementaryVC, SecondaryVC>(
_ primary :PrimaryVC,
_ supplementary :SupplementaryVC,
_ secondary :SecondaryVC)where PrimaryVC :ViewController,
SupplementaryVC :ViewController,
SecondaryVC :ViewController{self.init(style:.tripleColumn, viewControllers:[AnyViewController(primary),AnyViewController(supplementary),AnyViewController(secondary)])addChild(primary)addChild(supplementary)addChild(secondary)}conveniencepublicinit<PrimaryVC, SecondaryVC>(_ primary :PrimaryVC,
_ secondary :SecondaryVC)where PrimaryVC:ViewController, SecondaryVC:ViewController{self.init(style:.doubleColumn, viewControllers:[AnyViewController(primary),AnyViewController(secondary)])addChild(primary)addChild(secondary)}
// MARK: - View
publicstructContentView:View{@EnvironmentObjectprivatevarviewController:SplitViewControllerpublicinit(){}structEmbedChild:SwiftUI.View{letvc:_ViewController?varbody:someView{iflet vc = vc {
vc.anyControlledContentView
}}}publicvarbody:someView{
// SwiftUI switches the mode based on the _static_ style of the View
switch viewController.style {case.doubleColumn:NavigationView{EmbedChild(vc: viewController.children.first)EmbedChild(vc: viewController.children.count >1? viewController.children.dropFirst().first
:nil)}case.tripleColumn:NavigationView{EmbedChild(vc: viewController.children.first)EmbedChild(vc: viewController.children.count >1? viewController.children.dropFirst().first
:nil)EmbedChild(vc: viewController.children.count >2? viewController.children.dropFirst(2).first
:nil)}}}}}publicextensionAnyViewController{@inlinable // Note: not a protocol requirement, i.e. dynamic!
varsplitViewController:_SplitViewController?{
viewController.splitViewController
}}publicextension_ViewController{
/**
* Return the ``SplitViewController`` presenting/wrapping this controller.
*/
varsplitViewController:_SplitViewController?{
/// Is this VC itself being presented?
iflet presentingVC = presentingViewController {iflet nvc = presentingVC as?_SplitViewController{return nvc }return presentingVC.splitViewController
}iflet parent = parent as?_SplitViewController{return parent
}return parent?.splitViewController
}}
The text was updated successfully, but these errors were encountered:
For common master/detail setups. A SwiftUI
NavigationView
really is a UI/NSSplitView already :-) But the semantics wrt toshow
andshowDetail
would be different.Implementation shouldn't be too hard, depending on how many features are to be replicated.
A first attempt, to be finished:
The text was updated successfully, but these errors were encountered: