标签: swiftui-navigationlink

如何在 SwiftUI 中使用带条件检查的按钮进行导航

由于NavigationButton不再可用,我如何检查NavigationLink中的条件以便导航到另一个视图?

NavigationLink(destination: Dashboard(userName: self.userId,
                                      password: self.password), isActive: $showDashboard) {
    Button(action: {
        if self.userId.isEmpty || self.password.isEmpty {
            self.isAlert = true
        } else {
            self.showDashboard = true
        }
                       
    }) {
        Text("Submit")
            .foregroundColor(.white)
            .font(.system(size: 22))
                       
        Dashboard()
    }
    .frame(minWidth: 150, idealWidth: 300, maxWidth: 450,
           minHeight: 30, idealHeight: 40, maxHeight: 50, alignment: .center)
    .background(Color(red: 81/255, green: 221/255, blue: 182/255))
                       
    .padding([.leading, .trailing], 20)
}
Run Code Online (Sandbox Code Playgroud)

编辑:-

另外,如果用户名密码的长度超过 16,我想显示警报;如果长度超过 10,则显示不同的警报;如果长度为 0,则显示空消息警报。

navigation swiftui swiftui-navigationlink

3
推荐指数
1
解决办法
5309
查看次数

如何使用 SwiftUI 将手机锁定为纵向模式?

我正在 SwiftUI 中制作一个应用程序,但我不希望用户能够旋转屏幕并能够以横向模式查看该应用程序。我怎样才能做到这一点?

谢谢。

ios landscape-portrait swift swiftui swiftui-navigationlink

3
推荐指数
1
解决办法
1472
查看次数

无法更改 SwiftUI 中视图的背景颜色

我正在尝试更改此视图的背景颜色,但无法做到这一点。我尝试将背景(Color.green)放在HStack、VSTack甚至ZStack上,但它不起作用,不确定我是否放在正确的位置。默认情况下,它采用手机或模拟器颜色,即白色,但我想应用自定义背景颜色我的 Xcode 版本是 11.5

struct HomePageView: View {
    @State var size = UIScreen.main.bounds.width / 1.6
    
    var body: some View {
        GeometryReader{_ in
            VStack {
                HStack {
                    ZStack{
                        // main home page components here....
                        NavigationView{
                            VStack {
                                AssignmentDaysView()
                            }.background(Color.lairBackgroundGray)
                                .frame(width: 100, height: 100, alignment: .top)
                                .navigationBarItems(leading: Button(action: {
                                    self.size = 10
                                    
                                }, label: {
                                    Image("menu")
                                        .resizable()
                                        .frame(width: 30, height: 30)
                                }).foregroundColor(.appHeadingColor), trailing:
                                    Button(action: {
                                        print("profile is pressed")
                                    }) {
                                        HStack {
                                            NavigationLink(destination: ProfileView()) {
                                                LinearGradient.lairHorizontalDark
                                                    .frame(width: 30, height: 30)
                                                    .mask(
                                                        Image(systemName: …
Run Code Online (Sandbox Code Playgroud)

background ios swift swiftui swiftui-navigationlink

3
推荐指数
1
解决办法
8502
查看次数

SwiftUI 工具栏未显示在 NavigationLink 视图上

我试图在导航链接内部的视图上显示工具栏。当我导航到第三个视图时,我收到以下消息:

2020-09-15 23:09:31.097289-0500 CountTime[35018:3542166] [Assert] displayModeButtonItem 是内部管理的,不会公开为 DoubleColumn 样式。返回一个空的、断开连接的 UIBarButtonItem 来履行非空契约。

并且工具栏未显示。这种情况仅发生在 iPhone 上,而不是 iPad 上。我正在使用 Xcode 12 GM。

这是代码:

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: SecondView(),
                label: {
                    Text("Navigate")
                })
        }
    }
}

struct SecondView: View {
    var body: some View {
        ZStack {
            NavigationLink(
                destination: Text("Destination")
                    .toolbar {
                        ToolbarItem(placement: ToolbarItemPlacement.bottomBar) {
                                Button(action: {
                                    print("sharing")
                                }) {
                                    Image(systemName: "square.and.arrow.up")
                                }
                        }
                    },
                label: {
                    Text("Navigate")
                })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui swiftui-navigationlink xcode12

3
推荐指数
1
解决办法
4677
查看次数

在没有导航链接的情况下更改视图 SwiftUI

我大家!我花了几个小时寻找一些我认为非常简单的东西,但我无法找到最好的方法......我有我的身体视图:

var body: some View {
        
        VStack {
            // The CircularMenu
            CircularMenu(menuItems: homeMenuItems, menuRadius: 55, menuButtonSize: 55, menuButtonColor: .black, buttonClickCompletion: buttonClickHandler(_:))
                .buttonStyle(PlainButtonStyle())

        }
    }
Run Code Online (Sandbox Code Playgroud)

其中包含一个圆形菜单。每次单击菜单项都会调用:

func buttonClickHandler(_ index: Int) {
        /// Your actions here
        
        switch index {
        //Thermometer
        case 0:
            print("0")
        //Light
        case 1:
            print("1")
        //Video
        case 2:
            print("2")
        //Alarm
        case 3:
            print("3")
        //Car
        case 4:
            self.destinationViewType = .car
            self.nextView(destination: .car)
            
        default:
            print("not found")
        }
    }
Run Code Online (Sandbox Code Playgroud)

我想执行一个简单的视图转换到另一个名为“汽车”的视图。nextView函数如下所示:

func nextView(destination: DestinationViewType) {
        
        switch destination {
            case .car: Car()
        } …
Run Code Online (Sandbox Code Playgroud)

segue swiftui swiftui-navigationlink

3
推荐指数
1
解决办法
4246
查看次数

SwiftUI:如何使 NavigationLink 不会去任何地方并停留在当前视图上

我正在我的应用程序中进行一些表单验证,因此除非满足某些条件,否则我不希望导航链接转到指定的目的地。那么是否可以以某种方式将目的地设置为零或类似的值,以便在按下导航链接时不满足条件时它保留在当前视图上?

ios swift swiftui swiftui-navigationlink

3
推荐指数
1
解决办法
1828
查看次数

SwiftUI:列表、NavigationLink 和徽章

我正在开发我的第一个 SwiftUI 应用程序,并希望在其中显示一个List类别,并用一个徽章指示该类别中的项目数量。类别的标题将位于左侧,徽章将在行中右对齐。该列表将由NavigationLinks 组成,因此点击其中一个将进一步深入到视图层次结构。我编写的用于渲染 s 的代码NavigationLink如下所示:

        List {
            ForEach(myItems.indices) { categoryIndex in
                let category = categories[categoryIndex]
                let title = category.title
                let fetchReq = FetchRequest<MyEntity>(entity: MyEntity(),
                                            animation: .default)
                
                NavigationLink(destination: MyItemView()) {
                    HStack(alignment: .center) {
                        Text(title)
                        Spacer()
                        ZStack {
                            Circle()
                                .foregroundColor(.gray)
                            
                            Text("\(myItemsDict[category]?.count ?? 0)")
                                .foregroundColor(.white)
                                .font(Font.system(size: 12))
                        }
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

虽然它确实呈现了一个功能性的NavigationLink,但徽章并没有像我所希望的那样右对齐显示。相反,它看起来像这样:

在此输入图像描述

我知道我被某些事情所困扰HStack,但不确定是什么。如何才能使类别标题文本占据行的大部分,并且徽章在行中右对齐?

swift swiftui hstack swiftui-navigationlink

3
推荐指数
1
解决办法
594
查看次数

SwiftUI:如何横向呈现全屏覆盖

我正在制作一个应用程序,其中多个页面/视图嵌入到具有选项卡栏、菜单栏和标题的父视图中,如下所示:

       HeaderView()
       MenuView()
       TabView(selection: $selected){
          HomeView()
          Page2View()
          Page3View()
          Page4View()
      }
Run Code Online (Sandbox Code Playgroud)

当用户单击其中一页上的列表单元格时,我希望它能够横向打开详细信息视图,而不是作为垂直打开的工作表/模式打开,从而隐藏菜单栏、标题和选项卡栏。我已经尝试使用 FullScreenCover 来实现此目的,但它总是自下而上地呈现,而且我不知道如何从侧面呈现它。我也尝试过使用 NavigationLink,但是当我不希望显示标签栏、菜单栏和标题时,它会保留它们,并且我不知道如何使详细信息视图全屏显示。

您是否知道有什么方法可以使 FullsScreenCover 横向打开,或者让 NavigationLink 在另一个视图中填充整个屏幕?

swift swiftui swiftui-navigationlink

3
推荐指数
1
解决办法
8031
查看次数

如何在 SwiftUI 中动态推送视图或模态呈现视图?

我正在学习 SwiftUI,目前的重点是实现一个我能够使用 UIKit 实现的方法。我需要创建一个方法来确定是否应该推送视图或基于布尔值以模式方式呈现视图。

在 UIKit 中,我的代码是:

var presentVC = true // boolean that determines whether VC will be presented or pushed

let vc = ViewController() //Your VC that will be pushed or presented

if (presentVC == true) {
     self.presentViewController(vc, animated: true, completion: nil)
} else {
    self.navigationController.pushViewController(vc, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

但在 SwiftUI 中,我不确定如何使用以下方法正确实现:

  • NavigationLink - 用于推送视图
  • .sheet(isPresented:, content:) - 用于以模态方式呈现视图

看来 NavigationLink 和 .sheet 修饰符与视图实现相结合。有人在 SwiftUI 中遇到过并解决过这种情况吗?谢谢

我正在使用 SwiftUI 1.0,因为我需要支持 iOS 13。

presentmodalviewcontroller swiftui swiftui-navigationlink swiftui-navigationview

3
推荐指数
1
解决办法
3340
查看次数

SwiftUI NavigationLink 控制台错误(SwiftUI 在推送 aNavigationLink 时遇到问题。请提交错误。)

尝试以编程方式触发导航链接时遇到上述错误的问题。单击链接后,错误的目标数组元素将偶尔加载(相对于单击的标签),当然会显示上述错误。

当我以非编程方式触发导航链接时,不会发生错误,但当用户单击此链接时,我需要运行另一种方法。

我已经阅读了几个问题,例如此处此处,但找不到我的案例的原因。

模型结构:

struct Place: Identifiable {
var id: UUID = UUID()
var name: String = ""
var testBool: Bool = false
Run Code Online (Sandbox Code Playgroud)

}

环境对象的类:(生产项目有许多属性和方法 - 本质上这是 ViewModel)

class UserPlaces: ObservableObject {
@Published var placesArray: [Place] = [Place]([Place(name: "Christian", testBool: false),Place(name: "Catherine", testBool: true)])
Run Code Online (Sandbox Code Playgroud)

}

导航布尔类:(生产项目这里有许多属性,我想将它们保留在一个类中)

class Navigation: ObservableObject {
@Published var showPlaceDetail: Bool = false
Run Code Online (Sandbox Code Playgroud)

}

ContentView:(NavigationView所在的父视图)

import SwiftUI
Run Code Online (Sandbox Code Playgroud)

结构ContentView:视图{

var body: some View {
    
    NavigationView {
        
        ScrollView {
            
            Text("Hello, world!")
                .padding()
            CardsGrid2()
            
        } // …
Run Code Online (Sandbox Code Playgroud)

swiftui swiftui-navigationlink

3
推荐指数
1
解决办法
1873
查看次数