How to display a fullstack modal with SwiftUI?

戏子无情 提交于 2020-12-31 06:49:41

问题


I have a navbar up to and a tab bar at the bottom for most of the navigation in my app. I'm trying to display a fullscreen view, effectively removing all navigation, when clicking a button.

Other questions on SO address this case when there is no navbar or no tab bar, so they don't really answer my use case.

Here what I have tried:

  • Hide the navbar & tab bar on click: doesn't work for the tab bar. The top navigation becomes buggy if I try to go back to another view when dismissing the full screen view.

  • Use a modal: it works, but it's not fullscreen, which doesn't fit what I trying to do.

  • Use a ZStack on top of everything that I toggle with a button: this doesn't hide the tabbar.

Is there a solution, or should I just give up and use a modal?

Thanks


回答1:


Here is a demo of possible solution.

struct DemoModalOverTabView: View {
    @State private var showModally = false
    var body: some View {
        ZStack {
            TabView {     //  << main tab view
                DemoTab
            }.disabled(showModally) // << deactivate forcefully

            if showModally {
                DemoModal     //  << modal view
                   .zIndex(1)     // << required !!
                   .transition(.move(edge: .bottom)).animation(.default)
            }
        }
    }

    var DemoTab: some View {
        Button("Show") { self.showModally = true }
            .tabItem { Image(systemName: "person") }
    }

    var DemoModal: some View {
        Button("Hide") { self.showModally = false }
           .frame(maxWidth: .infinity, maxHeight: .infinity)
           .background(Color.yellow)
           .edgesIgnoringSafeArea(.all)
    }
}


来源:https://stackoverflow.com/questions/61807625/how-to-display-a-fullstack-modal-with-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!