有没有办法在 SwiftUI 中为 TextField 设置 inputView? [英] Is there any way to set inputView for TextField in SwiftUI?

查看:42
本文介绍了有没有办法在 SwiftUI 中为 TextField 设置 inputView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将选择器设置为 TextField 的 inputView,我可以仅使用 SwiftUI 来完成,还是必须在框架集成的帮助下使用 UIKit 组件?

I want to set picker as my inputView for TextField, can I do it with SwiftUI only or I have to use UIKit components with help of framework integration?

UIKit 中的代码示例:

Code example in UIKit:

textField.inputView = UIPickerView()

我想做同样的事情,但使用 SwiftUI 的 TextField

I want to do same, but with SwiftUI's TextField

推荐答案

从 Xcode 11.4 开始,SwiftUI 的 TextField 没有与 inputView 等效的 属性>UITextField.

As of Xcode 11.4, SwiftUI's TextField does not have an equivalent of the inputView property of UITextField.

您可以通过将 UIKit UITextField 桥接到 SwiftUI,以及通过将 SwiftUI Picker 桥接到 UIKit 来解决这个问题.您需要设置文本字段的 inputViewController 属性而不是其 inputView 属性.

You can work around it by bridging a UIKit UITextField to SwiftUI, and by bridging a SwiftUI Picker to UIKit. You'll need to set the text field's inputViewController property rather than its inputView property.

使用 UIViewRepresentableUITextField 包装在 SwiftUI View 中.由于您创建了 UITextField,您可以将其 inputViewController 属性设置为您创建的 UIViewController.

Use UIViewRepresentable to wrap the UITextField in a SwiftUI View. Since you create the UITextField, you can set its inputViewController property to a UIViewController that you create.

使用UIHostingController 将 SwiftUI Picker 包装在 UIViewController 中.将文本字段的 inputViewController 设置为您的 UIHostingController 实例.

UseUIHostingController to wrap a SwiftUI Picker in a UIViewController. Set the text field's inputViewController to your UIHostingController instance.

这篇关于有没有办法在 SwiftUI 中为 TextField 设置 inputView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆