在 SwiftUI 中打开新视图的按钮 [已关闭]

问题描述 投票:0回答:1

我正在尝试使用

button
来触发 SwiftUI 中的另一个
View

我不想使用

NavigationStack
NavigationLink
,因为我对此了解不多。

而是,有一个简单的

button
可以带来另一种观点。

是否可以仅使用

button
创建这样的操作(显示另一个视图)以及如何创建?

提前非常感谢,上帝保佑我们大家。 :)

button swiftui swiftui-navigationlink swiftui-sheet
1个回答
0
投票

根据您要查找的内容,您可以创建视图并使其显示为工作表并通过按钮消失。只需将视图放在上面即可。查看此代码以查看结果。您也可以只使用 if-else 语句来显示工作表。

enter image description here

struct ContentView: View {
    @State var showSheet:Bool = false
    var body: some View {
        GeometryReader {
            let size = $0.size
            //MARK: Back view
            VStack{
                Text("First View")
                    .foregroundStyle(.primary)
            }
            .frame(width: size.width, height: size.height, alignment: .center)
               
            //MARK: Sheet view
                VStack {}
                .frame(width: size.width, height: size.height, alignment: .center)
                
                .background(.ultraThinMaterial)
                .clipShape(RoundedRectangle(cornerRadius: 50))
                .shadow(radius: 10)
                .scaleEffect(0.98, anchor: .center)
                .offset(y:showSheet ? 30 : size.height + 100)
                .animation(.spring(.bouncy, blendDuration: 2.0), value: showSheet)
            //MARK: Action Button
            VStack{
                Button(action: {showSheet.toggle()}, label: {
                    Text("Button")
                })
                .buttonBorderShape(.roundedRectangle)
                .buttonStyle(BorderedProminentButtonStyle())
            }
            .frame(maxWidth: .infinity, alignment: .center)
        }
        .frame(maxWidth: .infinity, alignment: .center)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.