首页 > 解决方案 > 你能让 SwiftUI 从右到左以外的不同方向打开视图吗?

问题描述

我有代码:

NavigationLink(destination: ContentView(), label: {
    Text("Main Menu")
        .font(Font.custom("ComicNeue-Bold", size: 20))
        .foregroundColor(.white)
        .padding()
})

每当我按下它时,ContentView() 就会从右侧滑入。即使我添加

.transition(.move(edge: .leading))

到 NavigationLink 它仍然从右侧滑入。可以从左边打开吗?或任何其他方向?

标签: swiftanimationswiftuiswift5

解决方案


我不知道使用导航视图改变滑动方向(一般的滑动行为)的方法。但是,如果您想要自定义转换,我建议使用 GeometryReader 像这样

@State private var index: Int = 0

var body: some View {
    
    GeometryReader { _ in
        
        if self.index == 0 {
            
            FirstView().transition(.move(edge: .leading)
            
        } else {
            
            SecondView().transition(.move(edge: .leading)
            
        }
        
    }
    
}

推荐阅读