sheet swiftui

struct ContentView: View {
    @State var showingDetail = false

    var body: some View {
        Button(action: {
            self.showingDetail.toggle()
        }) {
            Text("Show Detail")
        }.sheet(isPresented: $showingDetail) {
            DetailView()
        }
    }
}

0
0
Snap 115 points

                                    // To show a modal (iOS 13 style), You just need a simple sheet with the ability to dismiss itself:

struct ModalView: View {
    @Binding var presentedAsModal: Bool
    var body: some View {
        Button("dismiss") { self.presentedAsModal = false }
    }
}
// And present it like:

struct ContentView: View {
    @State var presentingModal = false

    var body: some View {
        Button("Present") { self.presentingModal = true }
        .sheet(isPresented: $presentingModal) { ModalView(presentedAsModal: self.$presentingModal) }
    }
}

// Note that I passed the presentingModal to the modal so you can dismiss it from the modal itself, but you can get rid of it.
// To make it REALLY present fullscreen (Not just visually)
// You need to access to the ViewController. So you need some helper containers and environment stuff:

struct ViewControllerHolder {
    weak var value: UIViewController?
}

struct ViewControllerKey: EnvironmentKey {
    static var defaultValue: ViewControllerHolder {
        return ViewControllerHolder(value: UIApplication.shared.windows.first?.rootViewController)

    }
}

extension EnvironmentValues {
    var viewController: UIViewController? {
        get { return self[ViewControllerKey.self].value }
        set { self[ViewControllerKey.self].value = newValue }
    }
}

//Then you should use implement this extension:

extension UIViewController {
    func present<Content: View>(style: UIModalPresentationStyle = .automatic, @ViewBuilder builder: () -> Content) {
        let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
        toPresent.modalPresentationStyle = style
        toPresent.rootView = AnyView(
            builder()
                .environment(\.viewController, toPresent)
        )
        self.present(toPresent, animated: true, completion: nil)
    }
}

// Finally you can make it fullscreen like:

struct ContentView: View {
    @Environment(\.viewController) private var viewControllerHolder: UIViewController?

    var body: some View {
        Button("Login") {
            self.viewControllerHolder?.present(style: .fullScreen) {
                Text("Main") // Or any other view you like
            }
        }
    }
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
swift ui sheet how to open new view in swiftui swiftui recreate view using sheets in swiftui sheets in swift ui swift ui sheets swift present view how to open new view using swiftui swiftui contionally show different view swiftui custome sheet sheet in swiftui swiftui push a new view swiftui new view ios swiftui present view swiftui 2 present new view add view in swiftui swiftUI create view swiftui show new view swift cheat sheet swift sheet for swiftui sheet for sheet is presented swiftui how to add sheet swiftui swiftui display new view programmatically swiftui display new view swiftui sheet present view swiftui swiftui present sheet how to present view in swiftui how does sheet work swiftui swift present in uiview Swift .sheet swiftui .sheet presentmodal swiftui swiftui present view modally modal view tutorial swiftui modealview swiftui pop up sheet swiftui present modal swiftui swift how to get the current view controller to present uialert how to present uiview swift Go to new view in swiftui swiftui how to create a new view swiftui present modal swiftui present view swiftui button action present show sheet in swift ui sheets swiftui use Text with modal in swiftui modal view swiftui .sheet swiftui swiftui show modal view swiftui present view with button button swiftui modal view swiftui modal presentation present view in swiftui swiftui modal swiftui present modally button present new view controller swiftui present new view swiftui button tap swiftui modal modal view controller swiftui swift ui presenting view swift ui present another view controlelr action swiftui modal sheet swiftui show sheet sheet swiftui swiftui sheet how to present a view in swiftui swiftui present new view
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source