Is there any way to use storyboard and SwiftUI in same iOS Xcode project?
I just started to look at the SwiftUI
. Sharing a small example.
- In the
storyboard
addHosting View Controller
- Subclass the
UIHostingController
with your own class (ChildHostingController
) ChildHostingController
should look something like that:
import UIKit
import SwiftUI
struct SecondView: View {
var body: some View {
VStack {
Text("Second View").font(.system(size: 36))
Text("Loaded by SecondView").font(.system(size: 14))
}
}
}
class ChildHostingController: UIHostingController<SecondView> {
required init?(coder: NSCoder) {
super.init(coder: coder,rootView: SecondView());
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
For more details have a look at Custom UIHostingController
Apple Docs UIhostingController (Unfortunatelly it hasn't been documented yet)
Integrating SwiftUI Video
How to add Storyboard ViewController into SwiftUI Project?
You just created controller by class initialiser, to instantiate it from storyboard you have to do like the following
func makeUIViewController(context:
UIViewControllerRepresentableContext<AssetsListView>) -> AssetsListViewController {
let storyboard = UIStoryboard(name: "Main", // < your storyboard name here
bundle: nil)
let assetsListVC = storyboard.instantiateViewController(identifier:
"AssetsListViewController") // < your controller storyboard id here
assetsListVC.taskID = taskID
return assetsListVC
}
Integrating UIViewController with Storyboard into SwiftUI Project: A Gap at the Top after Transition
I assume it is empty large header area of NavigationView
, so just hide it
NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation()) {
Text("Tap me")
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
Is there any way to use storyboard and SwiftUI in same iOS Xcode project for Apple Watch?
WKHostingController is the WatchKit equivalent of UIHostingController
.
If you use WKHostingController
, you'll be able to use a SwiftUI view in a storyboard on watchOS.
Related Topics
Countdown with Several Decimal Slots, Using Nstimer in Swift
Navigationlink Inside Lazyvgrid Cycles All Entries on Back, Swiftui
Changing Constraint When Keyboard Appears - Swift
Cannot Invoke 'Decode' with an Argument List of Type '(T, From: Data)'
Convert Uiview to .Png in Swift
Wkwebview Is Showing Only Background
How to Find the Index of a Tuple Element from an Tuple Array? iOS, Swift
How to Create a "Hyperlink" with Swift
Swift and Nsuserdefaults - Exc_Bad_Instruction When User Defaults Empty
Save Video to a Custom Album Using Photos Framework in iOS
How to Add Two or More Buttons to Annotationview: Mkannotationview
A Simple Code to Detect Any Beacon in Swift
Uitableviewcell Initwithstyle:Uitableviewcellstylesubtitle Is Not Working
Swift3: Live Uilabel Update on User Input
Wkwebview Decidepolicyfornavigationaction Being Call After the Long Press Recogniser Ended