Add a prefix to TextField in SwiftUI

后端 未结 5 539
我寻月下人不归
我寻月下人不归 2021-01-22 07:26

I want a ‘+’ Sign in the Textfield which cannot be erased. A user should be able to enter values after it and if he presses backspace it should only erase the v

5条回答
  •  孤城傲影
    2021-01-22 07:58

      import SwiftUI
    
    struct ContentView: View {
    
        @State var userName : String = ""
        var body: some View {
            VStack {
                HStack{
                    Image(systemName: "plus")
                    TextField("placeholder",text:$userName)
                }
                .padding(5)
                .foregroundColor(.white)
                .background(Capsule().fill(Color.gray))
                Spacer()
            }.padding(5)
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    

    I will say this is better approach to add "+" sign , so that you will not receive + sign when you access userName

提交回复
热议问题