【问题标题】:Custom TextField Swiftui自定义 TextField Swiftui
【发布时间】:2023-01-10 02:33:40
【问题描述】:

如何创建如图所示的文本字段?有那种颜色和白线的背景?

import SwiftUI

@available(iOS 16.0, *)
struct SecondView: View {
    @State var nome = ""
    @State var cognome = ""

    
    var body: some View {
        GeometryReader { geo in
            ZStack {
                Color(red: 57 / 255, green: 63 / 255, blue: 147 / 255)
                Text("Ora tocca ai tuoi dati")
                    .font(Font.custom("Rubik", size: 22)).foregroundColor(Color.white).font(Font.body.bold()).padding(.bottom, 300).font(Font.headline.weight(.light))

                Text("Per iniziare ad usare MyEntZo completa i campi qui sotto").multilineTextAlignment(.center)
                    .font(Font.custom("Rubik", size: 18)).foregroundColor(Color.white).font(Font.body.bold()).padding(.bottom, 80).font(Font.headline.weight(.light))

                TextField("Nome", text: $nome, axis: .vertical)
                    .textFieldStyle(.roundedBorder)
                    .background(.ultraThinMaterial)
                    .padding().padding(.bottom, 20).padding(.top, 110).scaledToFill()
                    .underline()

                TextField("Cognome", text: $nome, axis: .vertical)
                    .textFieldStyle(.roundedBorder)
                    .background(.ultraThinMaterial)
                    .padding().padding(.bottom, 20).padding(.top, 190).scaledToFill()
                    .underline()
                
            
                    Button(action: {
                        print("sign up bin tapped")
                    }){
                        Text("Continua")
                            .frame(minWidth: 0, maxWidth: .infinity)
                            .font(.system(size: 18))
                            .padding()
                            .foregroundColor(.white)
                            .overlay(
                                RoundedRectangle(cornerRadius: 25)
                                    .stroke(Color.green, lineWidth: 2)
                            )
                    }
                    .background(Color.green)
                    .cornerRadius(25).frame(width: geo.size.width - 50, height: 100)
                    .padding(.top, 300)
                }
            }
            .ignoresSafeArea()
        
    }
}

struct SecondView_Previews: PreviewProvider {
    static var previews: some View {
        SecondView()
    }
}

【问题讨论】:

    标签: swift swiftui textfield


    【解决方案1】:

    您可以创建一个包含常规 TextField 的自定义文本字段,将 @State 作为 @Binding 传递,例如

    struct CustomTextField: View {
        
        let placeholder: String
        @Binding var text: String
        
        var body: some View {
            VStack {
                TextField(placeholder, text: $text)
                    .padding(.bottom, 4)
                    .foregroundColor(.white)
                Color.white
                    .frame(height: 2)
            }
            .padding([.top, .bottom, .trailing])
            .background(.blue)
        }
    }
    

    你可以像这样使用:

    struct ContentView: View {
        
        @State private var nome: String = ""
        
        var body: some View {
            VStack {
                // etc
                CustomTextField(placeholder: "Nome", text: $nome)
                // etc
            }
        }
    }
    

    【讨论】:

      【解决方案2】:
      struct SecondView: View {
          @State var nome = ""
          @State var cognome = ""
      
          
          var body: some View {
              GeometryReader { geo in
                  ZStack {
                      Color(red: 57 / 255, green: 63 / 255, blue: 147 / 255)
                      VStack {
                          Spacer()
                          Text("Ora tocca ai tuoi dati")
                              .font(Font.custom("Rubik", size: 22))
                              .foregroundColor(.white)
                              .bold()
                              .padding()
                              .font(Font.headline.weight(.light))
                          Text("Per iniziare ad usare MyEntZo completa i campi qui sotto").multilineTextAlignment(.center)
                              .font(Font.custom("Rubik", size: 18)).foregroundColor(Color.white).font(Font.body.bold()).padding(.bottom, 80).font(Font.headline.weight(.light))
                      
                          TextField("Nome", text: $nome, prompt: Text("Nome").foregroundColor(.white.opacity(0.7)))
                              .foregroundColor(.white)
                              .padding(.horizontal)
                          Rectangle()
                              .foregroundColor(.white)
                              .background(.white)
                              .frame(maxWidth: .infinity, maxHeight: 2)
                              .padding(.horizontal)
                          TextField("Cognome", text: $cognome, prompt: Text("Cognome").foregroundColor(.white.opacity(0.7)))
                              .foregroundColor(.white)
                              .padding([.horizontal, .top])
                          Rectangle()
                              .foregroundColor(.white)
                              .background(.white)
                              .frame(maxWidth: .infinity, maxHeight: 2)
                              .padding(.horizontal)
                          
                          Button(action: {
                              print("sign up bin tapped")
                          }){
                              Text("Continua")
                                  .frame(minWidth: 0, maxWidth: .infinity)
                                  .font(.system(size: 18))
                                  .padding()
                                  .foregroundColor(.white)
                                  .overlay(
                                      RoundedRectangle(cornerRadius: 25)
                                          .stroke(Color.green, lineWidth: 2)
                                  )
                          }
                          .background(Color.green)
                          .cornerRadius(25).frame(width: geo.size.width - 50, height: 100)
                          .padding(.top)
                          
                          Spacer()
                      }
                  }
                      
              }
              .ignoresSafeArea()
              
          }
      }
      

      【讨论】:

      • 你应该解释你对原始代码做了什么修改,以及它是如何解决问题的。这里不鼓励仅代码答案,因为它们不一定提供足够的上下文来帮助未来的读者。
      猜你喜欢
      • 2021-09-30
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      相关资源
      最近更新 更多