【问题标题】:SwiftUI TextField: Keyboard does not show up on tappedSwiftUI TextField:点击时键盘不显示
【发布时间】:2021-05-04 13:56:52
【问题描述】:

我在HStack 中有一个简单的TextField 和一个按钮

        HStack {
            TextField("Level Name", text: $levelName)
                .font(.title)
                .padding(10)
                .background(
                    RoundedRectangle(cornerRadius: 15)
                        .strokeBorder(Color.primary.opacity(0.5), lineWidth: 3))
            
            Button(action: {

            }) {
                Text("Submit").font(.title)
            }
        }

单击文本字段不会在我的模拟器上显示键盘,给控制台警告:

[LayoutConstraints] 无法同时满足约束。 以下列表中的至少一个约束可能是您不想要的。 尝试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 (注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档) ( "<0x600002bba8f0 h="--&" v="--&" _uibuttonbarbutton:0x7fdbcbc52e90.height="="><0x600002bb4050 _uiucbkbselectionbackground:0x7fdbcbc53890.bottom="=" _uibuttonbarbutton:0x7fdbcbc52e90.bottom><0x600002bbff70 v:>

<0x600002bb4050 _uiucbkbselectionbackground:0x7fdbcbc53890.bottom="=" _uibuttonbarbutton:0x7fdbcbc52e90.bottom>

标签: ios swift swiftui


【解决方案1】:

您的代码有效。键盘出现。运行 iOS 15.1。

struct TestView: View {
    
    @State var levelName = ""
    
    var body: some View {
           
        HStack {
            TextField("Level Name", text: $levelName)
                .font(.title)
                .padding(10)
                .background(
                    RoundedRectangle(cornerRadius: 15)
                        .strokeBorder(Color.primary.opacity(0.5), lineWidth: 3))
            
            Button(action: {
                
            }) {
                Text("Submit").font(.title)
            }
        }
        
    }
    
}

【讨论】:

    猜你喜欢
    • 2021-05-22
    • 2021-08-04
    • 1970-01-01
    • 2014-11-24
    • 2020-05-17
    • 2020-02-09
    • 1970-01-01
    • 2014-03-16
    • 2020-08-04
    相关资源
    最近更新 更多