I have one view named WelcomeView in SwiftUI which is having next button. so on click of next button I want to push my view to other view using NavigationLink.
Problem that I am facing is I have to render my WelcomeView inside UITableView Cell in Swift file. so when I render that page inside UITableView Cell, and click on join button it is pushing my view to next view but inside UITableViewCell only.
I want to show my next view in whole page and not inside table cell.
TableFile (Swift)
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = theTableview.dequeueReusableCell(withIdentifier: "SessionCardCell", for: indexPath) as! HostingTableViewCell<WelcomeView>
cell.host(WelcomeView(), parent: self)
return cell
}
Cell Class
class HostingTableViewCell<Content: View>: UITableViewCell {
private let hostingController = UIHostingController<Content?>(rootView: nil)
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
hostingController.view.backgroundColor = .clear
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: ScreenSize.width, height: 100 * ScreenSize.width/375)
// return hostingController.sizeThatFits(in: CGSize(width: ScreenSize.width, height: 250))
}
override func layoutSubviews() {
super.layoutSubviews()
hostingController.view.frame.size = CGSize(width: ScreenSize.width, height: 100 * ScreenSize.width/375)//self.sizeThatFits(bounds.size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func host(_ view: Content, parent: UIViewController) {
self.hostingController.rootView = view
self.hostingController.view.invalidateIntrinsicContentSize()
let requiresControllerMove = hostingController.parent != parent
if requiresControllerMove {
parent.addChild(hostingController)
}
if !self.contentView.subviews.contains(hostingController.view) {
self.contentView.addSubview(hostingController.view)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
hostingController.view.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
hostingController.view.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
hostingController.view.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
hostingController.view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
}
if requiresControllerMove {
hostingController.didMove(toParent: parent)
}
}
}
WelcomeView (SwiftUI root View)
struct WelcomeView: View {
@State var pushToNextScreen : Bool = false
var body: some View {
NavigationView {
VStack {
Button {
//action
self.pushToNextScreenRoot = true
} label: {
Text("Join")
}
NavigationLink(destination: SecondView(), isActive: self.$pushToNextScreenRoot) { }
}//vstack
}//navigation view
}
}