简体   繁体   English

如何使用 SwiftUI 中的按钮在屏幕之间导航

[英]How to navigate between screens using a button in SwiftUI

代码

Hello, I want to navigate between windows using a button but not use a NavigationLink .您好,我想使用按钮在 windows 之间导航,但不使用NavigationLink It looks ugly.它看起来很丑。 this is my code这是我的代码

import SwiftUI

struct ContentView: View {

    var body: some View {
        Button(action: action()){
            Text("Hola")
                .font(.largeTitle)
                .frame(width: 100, height: 100)
            .background(Color.red)
        }

    }
}

You can use an empty NavigationLink and bind your navigation flag or destination to your Button .您可以使用空NavigationLink并将您的导航标志或目的地绑定到您的Button

struct FirstView: View {
    
    @State var navigationFlag = false
    
    var body: some View {
        NavigationView {
            VStack {
                Text("First View")
                
                Button(action: {
                    self.navigationFlag = true
                }, label: {
                    Text("navigate")
                })
                
                NavigationLink(destination: SecondView(),
                               isActive: self.$navigationFlag,
                               label: {
                                EmptyView()
                               })
            }
        }
    }
    
}

struct SecondView: View {
    
    var body: some View {
        Text("Second View")
    }
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 SwiftUI 中的按钮在视图之间导航? - How do I navigate between views using a button in SwiftUI? 如何使用 SwiftUI 中的条件检查按钮进行导航 - How to navigate using button with condition check in SwiftUI 如何在swiftui中按钮操作的视图控制器之间导航? - How to navigate between view controllers on a button action in swiftui? 如何在 SwiftUI 中的两个 API 参数之间导航 - How to navigate between two API parameters in SwiftUI 如何使用 swiftUI 中的按钮在视图之间导航 - how to navigate between views with buttons in swiftUI 如何通过点击 ForEach 中的按钮导航到不同的屏幕/视图 - How to navigate to different screens/views by tapping button which is in ForEach 如何使用 SwiftUI 在多个屏幕上获取键盘高度并移动按钮 - How to get the keyboard height on multiple screens with SwiftUI and move the button 如何从 SwiftUI 中的导航栏按钮单击导航到新视图 - How to navigate to a new view from navigationBar button click in SwiftUI 如何使用 SwiftUI 中的按钮导航一个视图 - How can I navigate one view back with a button in SwiftUI 如何通过单击键盘上的返回按钮来浏览 SwiftUI 文本字段? - How to navigate through SwiftUI TextFields by clicking on return button from keyboard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM