【问题标题】:Expande clickable area on TextField in SwiftUI, Xcode 11.2在 SwiftUI、Xcode 11.2 中展开 TextField 上的可点击区域
【发布时间】:2020-02-25 08:16:45
【问题描述】:

无法弄清楚如何使可点击区域更大以在 swiftUI(iOS 13、Xcode 11.2)上打开键盘。我只能影响视觉外观,但不能影响用户可以点击的实际区域(可点击区域 == 无论占位符文本的 fontSize 是什么。)

在 swiftUI 中创建 TextField 时,可以使用 frame() 来增加占位符文本周围的大小,我也可以使用 fontsize 使框内的字体更大,但没有什么可以使可点击区域(调出键盘)更大没有使字体更大。

//用于修改TextFields的代码

struct SignInModifier: ViewModifier {

func body(content: Content) -> some View {
    return content
        .padding(.all).font(.system(size: 18)).border(Color.purple).foregroundColor(Color.purple).shadow(radius: 2).frame(width: 350, height: 50)
    }
}

//我在哪里调用修饰符

TextField("email address", text: $email).modifier(SignInModifier()).disableAutocorrection(true).keyboardType(.emailAddress)

我希望当您单击框架内的任何位置时,键盘会打开/您可以在文本字段中输入。但是,我需要单击占位符文本的上半部分才能在 TextField 中输入

【问题讨论】:

    标签: ios swift textfield swiftui


    【解决方案1】:

    有点解决方法,但有效。

     struct ContentView: View {
     @State var name = ""
     @State var isFocused = false
      var body: some View {
        ZStack {
            HStack{
                Text(name)
                    .font(.system(size: 50 , weight: .black))
                    .foregroundColor(isFocused ? Color.clear : Color.black)
                Spacer()
            }
            TextField(name, text: $name , onEditingChanged: { editingChanged in
                isFocused = editingChanged
            }) 
            .font(.system(size: isFocused ? 50 :  100 , weight: .black))
            .foregroundColor(isFocused ? Color.black  : Color.clear)
            .frame(width: 300, height: isFocused ? 50 :  100 , alignment: .center)
                        .padding(.leading, isFocused ? 25 : 0  )
                        .padding(.trailing, isFocused ? 25 : 0 ) 
            .padding(.top,isFocused ? 25 : 0 )
    
            .padding(.bottom,isFocused ? 25 : 0 )
            
        }.frame(width: 300)
        .background(Color.red.opacity(0.3))
       }
        }
    

    【讨论】:

      【解决方案2】:

      好的,所以我想出了一个解决方法,但并不完全是一个解决方案;只需将 TextField 嵌入到一个按钮中,无论您点击哪里,它都会立即打开

      【讨论】:

      • 我无法在 iOS 14.1 中确认此行为
      猜你喜欢
      • 2020-09-06
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2021-06-04
      相关资源
      最近更新 更多