【问题标题】:SwiftUI view over all the views including sheet view所有视图的 SwiftUI 视图,包括工作表视图
【发布时间】:2021-09-02 15:03:58
【问题描述】:

无论顶视图是什么,我都需要根据特定条件显示所有视图之上的视图。我正在尝试以下代码:

    struct TestView<Presenting>: View where Presenting: View {
    
        /// The binding that decides the appropriate drawing in the body.
        @Binding var isShowing: Bool
        /// The view that will be "presenting" this notification
        let presenting: () -> Presenting
    
        var body: some View {
    
            GeometryReader { geometry in
                ZStack(alignment: .top) {
                    self.presenting()
    
                    HStack(alignment: .center) {
                        Text("Test")
                    }
                    .frame(width: geometry.size.width - 44,
                           height: 58)
                    .background(Color.gray.opacity(0.7))
                    .foregroundColor(Color.white)
                    .cornerRadius(20)
                    .transition(.slide)
                    .opacity(self.isShowing ? 1 : 0)
                }
            }
        }
    }

extension View {
    func showTopView(isShowing: Binding<Bool>) -> some View {
        TestView(isShowing: isShowing,
                               presenting: { self })
    }    
}

struct ContentView: View {
   @State var showTopView = false
   NavigationView {
            ZStack {
                content
            }
        }
        .showTopView(isShowing: $showTopView)

}

现在,如果视图被推送,这可以正常工作。但我无法在呈现的视图上方显示此 TopView。

感谢任何帮助!

【问题讨论】:

  • 这是否回答了您的问题stackoverflow.com/a/63259094/12299030?
  • 不,它不是@Asperi。我需要一个视图显示在顶部,而不考虑当前的顶视图是什么,是呈现或推送的顶视图等。这就像在所有视图的顶部显示一个加载器。

标签: ios swiftui


【解决方案1】:

这里有一种方法可以实现你的目标,你不需要Binding,只需要使用let value。

struct ContentView: View {
    
    @State private var isShowing: Bool = Bool()
    
    var body: some View {
        
        CustomView(isShowing: isShowing, content: { yourContent }, isShowingContent: { isShowingContent })
        
    }
    
    var yourContent: some View {
        
        NavigationView {
            
            VStack(spacing: 20.0) {
                
                Text("Hello, World!")
                
                Button("Show isShowing Content") { isShowing = true }
                
            }
            .navigationTitle("My App")
            
        }
        
    }
    
    var isShowingContent: some View {
        
        ZStack {
            
            Color.black.opacity(0.5).ignoresSafeArea()
            
            VStack {
                
                Spacer()
                
                Button("Close isShowing Content") { isShowing = false }
                    .foregroundColor(.white)
                    .padding()
                    .frame(maxWidth: .infinity)
                    .background(Color.blue.cornerRadius(10.0))
                    .padding()
                
            }
            
        }
        
    }
    
}

struct CustomView<Content: View, IsShowingContent: View>: View {
    
    let isShowing: Bool
    @ViewBuilder let content: () -> Content
    @ViewBuilder let isShowingContent: () -> IsShowingContent
    
    var body: some View {
        
        Group {
            
            if isShowing { ZStack { content().blur(radius: isShowing ? 5.0 : 0.0); isShowingContent() } }
            else { content() }
 
        }
        .animation(.default, value: isShowing)
        
    }
}

【讨论】:

  • 在呈现视图的情况下它不起作用。呈现的视图上方没有显示任何内容。
  • 是的,我试过你的代码。请尝试使用工作表呈现某些内容,您会发现自定义视图永远不会显示在工作表上方。
  • stackoverflow.com/questions/59277120/… 这是我想要回答的问题
  • 有了我的答案,你不需要使用表!我的代码是工作表!
猜你喜欢
  • 2020-04-04
  • 1970-01-01
  • 2010-09-10
  • 2023-01-12
  • 2021-10-28
  • 1970-01-01
  • 2020-06-04
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多