用于使用新视图切换工作表的按钮

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

又是我!

感谢上帝的恩典,我希望有一个

button
可以触发
sheet
,从而完全显示另一个
UIView

我尝试了

NavigationStack
NavigationLink
,但它们让我的用户界面变得一团糟,我不知道如何解决。

是否可以使用

button
创建这样的操作以及如何创建?

感谢并愿上帝大大祝福你们所有人的支持。

ios 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.