Search code examples
iosiphoneswiftskyfloatinglabeltextfield

SkyFloatingLabelTextField auto move label up on focus


I am a newbie to this! I am using Swift 4 and I have configured the plugin called SkyFloatLabelTextField.

I was wondering if someone else has solved how to auto move the label up if the cursor is on focus? I want the label to move up the moment you select a text field. At the moment you have to type for the label to move up.

Thank you.


Solution

  • You can achieve this functionality by subclassing SkyFloatingLabelTextField and managing title visibility.

    final class MovingTitleOnFocusTextField: SkyFloatingLabelTextField {
    
        override func becomeFirstResponder() -> Bool {
            setTitleVisible(true)
            return super.becomeFirstResponder()
        }
    
        override func resignFirstResponder() -> Bool {
            setTitleVisible(hasText || hasErrorMessage)
            return super.resignFirstResponder()
        }
    }
    

    If you use SkyFloatingLabelTextFieldWithIcon you have to subclass that as well, but same idea.