我想知道我可以在 tvOS 上的 TextField 中自定义哪些内容。据我所知,我只能使用
SubmitLabel
稍微更改标题和按钮标题
但是,在查看“设置”界面时,我注意到苹果让它变得更好了一些。它包含描述、标题/描述的不同颜色、按钮的自定义标题,并且当文本字段为空时按钮将被禁用。
有可能实现所看到的这些事情吗?
我尝试使用设计键盘输入体验中的inputAccessoryView来添加另一个按钮,它可以在UIKit上运行,但它不会在点击时发送任何操作。
所以我的代码现在看起来像这样
import SwiftUI
struct ContentView: View {
@State private var text = ""
var body: some View {
TextField("TitleKey", text: $text)
.submitLabel(.continue)
}
}
而且我不知道如何进一步自定义此视图。
在 Apple TV 的 SwiftUI 中,
SecureField
和 TextField
有一个限制,即仅接受简单文本作为标签。这是 SecureField 的文档:
public struct SecureField<Label> : View where Label : View {
要提供更详细的标签或说明,实用的解决方法是在
Text
之前 条目上方使用单独的 SecureField
视图。例如:
struct ContentView: View {
@State private var text = ""
var body: some View {
VStack(spacing: 16) {
Text("Apple ID Password")
.font(.title3)
Text("Enter the password for \"asda\".")
.foregroundStyle(.secondary)
.padding(.bottom, 100) // Arbitrary number, might be better to size it with spacing
SecureField(text: $text) {
Text("Password")
}
.submitLabel(.continue)
.frame(width: 600) // Arbitrary number, might be better to size it with spacing
}
}
}
此方法使用额外的
Text
视图作为上下文,具有可自定义的间距和布局,有效解决 SecureField
标签限制。