'init(destination:isActive:label:)' 在 iOS 16.0 中已弃用:在 NavigationStack 或 NavigationSplitView 中使用 NavigationLink(value:label:)

Liz*_*izG 21 deprecation-warning swiftui-navigationlink ios16 xcode14

我已经尝试了该网站上发布的示例的不同版本,但没有任何进展。

我也尝试过遵循此处列出的示例:

导航到新的导航类型:

我在这篇文章的标题中收到关于弃用 NavigationLink(destination:isActive) 的警告

struct PhoneLogin: View {
    @StateObject var phoneLoginData = PhoneLoginViewModel()
    @State var isSmall = UIScreen.main.bounds.height < 750
    
    var body: some View {
        VStack {
            VStack {
                Text("Continue With Phone")
                    .font(.title2)
                    .fontWeight(.bold)
                    .foregroundColor(.black)
                    .padding()
                
                Image("phone_logo")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .padding()
                
                Text("You'll receive a 4-digit code \n to verify next")
                    .font(isSmall ? .none : .title2)
                    .foregroundColor(.gray)
                    .multilineTextAlignment(.center)
                    .padding()
                
                // Mobile Number Field . . . . .
                
                HStack {
                    VStack(alignment: .leading, spacing: 6) {
                        Text("Enter Your Number")
                            .font(.caption)
                            .foregroundColor(.gray)
                        
                        Text("+ \(phoneLoginData.getCountryCode()) \(phoneLoginData.phNo)")
                            .font(.title2)
                            .fontWeight(.bold)
                            .foregroundColor(.black)
                    }
                    
                    Spacer(minLength: 0)

                    NavigationLink(
                        destination: Verification(phoneLoginData: phoneLoginData), isActive: $phoneLoginData.goToVerify) {
                        
                        Text("")
                            .hidden()
                    }
                    
                    Button(action: phoneLoginData.sendCode, label: {
                        Text("Continue")
                            .foregroundColor(.black)
                            .padding(.vertical, 18)
                            .padding(.horizontal, 38)
                            .background(Color.yellow)
                            .cornerRadius(15)
                    })
                    
                    .disabled(phoneLoginData.phNo == "" ? true : false)
                }
                .padding()
                .background(Color.white)
                .cornerRadius(20)
                .shadow(color: Color.black.opacity(0.1), radius: 5, x: 0, y: -5)
            }
            .frame(height: UIScreen.main.bounds.height / 1.8)
            .background(Color.white)
            .cornerRadius(20)
            
            // Custom Number Pad
            CustomNumberPad(value: $phoneLoginData.phNo, isVerify: false)
        }
        .background(Color("bg").ignoresSafeArea(.all, edges: .bottom))
    }
}
Run Code Online (Sandbox Code Playgroud)

Liz*_*izG 37

我终于弄明白了:

更换:

NavigationLink(
     destination: Verification(phoneLoginData: phoneLoginData), 
     isActive: $phoneLoginData.goToVerify) {
          Text("")
               .hidden()
     }
Run Code Online (Sandbox Code Playgroud)

和:

确保以下代码放置在 NavigationStack (ios 16) 内的任何位置:

.navigationDestination(
     isPresented: $phoneLoginData.goToVerify) {
          Verification(phoneLoginData: phoneLoginData)
          Text("")
              .hidden()
     }
Run Code Online (Sandbox Code Playgroud)

  • 请注意,`Text("").hidden()` 不是必需的,这会在应用程序上添加一个奇怪的黑色底部区域 (4认同)