Swift: MaterialComponents TextFields Unexpected Placeholder Behavior when clicking fast

2.6k Views Asked by At

I have open the issue at MaterialComponents on Github already but seem like no answer yet. The problem is when I click the textfield to be active then I click outside, it was working fine but when I repeat doing that, the placeholder for the textfield will be always on the top

Here is the problem as an image

When textfield didload: enter image description here

When textfield active: enter image description here

When we repeat click on textfield then click outside again it will look like this enter image description here

Here is the code:

@IBOutlet weak var textfieldFloating: MDCTextField!
let tf = MDCTextInputControllerOutlined()

override func viewDidLoad() {
        super.viewDidLoad()
        textfieldFloating.leftView = UIImageView(image: UIImage(named: "ic_lock_outline_white_18pt"))
        textfieldFloating.leftViewMode = .always
        textfieldFloating.placeholder = "Placeholder"
        tf.textInput = textfieldFloating
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
1

There are 1 best solutions below

1
On

I would personally go for https://github.com/raulriera/TextFieldEffects CocoaPod it has a lot of different styles which you can use, and best of all it is very simple tu use.

just create a pod file with pod init then add this:

use_frameworks!
pod 'TextFieldEffects'

to your pods and install it with pod install from terminal.

To use it:

The library is a simple drop-in, as soon as you set your subclass to one of the effects and your module to TextFieldEffects you will be able to see all the IBDesignable settings in the storyboard.

You can also use it programmatically without storyboards as you wish:

let textField = KaedeTextField(frame: textFieldFrame)
textField.placeholderColor = .darkGrayColor()
textField.foregroundColor = .lightGrayColor()

view.addSubView(textField)