繁体   English   中英

SwiftUI 文本字段:点击时键盘不显示

[英]SwiftUI TextField: Keyboard does not show up on tapped

我在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)(“<NSAutoresizingMaskLayoutConstraint:0x600002bba8f0 h=--& v=--& _UIButtonBarc90.heightb= (活动)>", "<NSLayoutConstraint:0x600002bb4050 _UIUCBKBSelectionBackground:0x7fdbcbc53890.bottom == _UIButtonBarButton:0x7fdbcbc52e90.bottom - 6 (活动)>", "<NSLayoutConstraint:0x600002bbff70 V:|-(6)-[0x7fdbcbc5890:]活动,名称:'|':_UIButtonBarButton:0x7fdbcbc52e90 )>" )

将尝试通过打破约束来恢复 <NSLayoutConstraint:0x600002bb4050 _UIUCBKBSelectionBackground:0x7fdbcbc53890.bottom == _UIButtonBarButton:0x7fdbcbc52e90.bottom - 6 (active)>

在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点以在调试器中捕获它。 <UIKitCore/UIView.h> 中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助。

您的代码有效。 键盘出现。 运行 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)
            }
        }
        
    }
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM