swiftswiftuitextfieldaspect-fit

Textfield background image Aspect fit is restricting the width in iPad in swiftUI


In iPhone designs looks fine. But in iPad, the TextField width is not increasing. And if I change the content mode to fill both fields are overlapping. How can I fix this?

    var body: some View {
       
        VStack (alignment: .center,spacing :35){
            
            TextField("Enter Username", text: $username).frame(minWidth: 0 , maxWidth: .infinity).frame(height: 55).background(Image("userinput").resizable().aspectRatio(contentMode: .fit)).multilineTextAlignment(.center).padding([.leading,.trailing],15)
                            
            TextField("Enter Password", text: $username).frame(minWidth: 0 , maxWidth: .infinity).frame(height: 55).background(Image("pswrd").resizable().aspectRatio(contentMode: .fit)).multilineTextAlignment(.center).padding([.leading,.trailing],15)
                
            Button("SIGN IN") {
               action = true
                print("Just tapped")
            }.frame(minWidth: 0 , maxWidth: .infinity).frame(height: 55).background(Color(red: 46/255, green: 152/255, blue: 182/255)).foregroundColor(.white).cornerRadius(5).padding([.leading,.trailing],50)
       
    }
}

Solution

  • The text field's width is definitely increasing (try adding a .border(Color.gray) to test). But, I assume your userinput or pswrd images look something like this?

    Gray rectangle with blue person icon on top at the left

    This is bad practice. You don't want to make your image contain dynamic stuff like the text field's border, because images are fixed and can't stretch.

    Instead, make your image only contain the blue person image, then use HStack to put it on the left.

    struct ContentView: View {
        @State var username = ""
        @State var password = ""
        
        var body: some View {
            VStack(alignment: .center, spacing: 35) {
                
                HStack {
                    Image("userinput")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                    
                    TextField("Enter Username", text: $username)
                        .multilineTextAlignment(.center)
                }
                .frame(height: 55)
                .border(Color.gray)
                .padding([.leading, .trailing], 15)
                
                HStack {
                    Image("pswrd")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                    
                    TextField("Enter Password", text: $password)
                        .multilineTextAlignment(.center)
                }
                .frame(height: 55)
                .border(Color.gray)
                .padding([.leading, .trailing], 15)
                
                Button("SIGN IN") {
                    print("Just tapped")
                }
                .frame(maxWidth: .infinity)
                .frame(height: 55)
                .background(Color(red: 46/255, green: 152/255, blue: 182/255))
                .foregroundColor(.white)
                .cornerRadius(5)
                .padding([.leading, .trailing], 50)
                
            }
        }
    }
    

    Result:

    iPhone iPad
    Text fields resize to full width on iPhone Text fields resize to full width on iPad