简体   繁体   中英

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.

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.

UPDATE 2

I have forked the project and did the necessary changes. You can check it out here . I also did a Pull Request to the original author so this functionality will maybe be added to the original project.

To use the behavior simply set myFloatingTextField.isAnimationOnTouch 0 true . This will cause the animation to happen right when you tap in the textfield.

Happy coding!

UPDATE 1

So the question was more related to the behavior of the SkyFloatingLabelTextField. Sorry for misunderstanding!

Following the very first bunch of code on the github page, the label will move up as soon as you type. So you want it right from the touch? Then you should fork the project, and adopt the code from

@objc open func editingChanged() {
    updateControl(true)
    updateTitleLabel(true)
}

to achieve it on the touch. This can be done in the delegate method func textFieldDidBeginEditing(_ textField: UITextField)

Once you have done this, I think you will be able to have the label move up once the user touches the textField.


Original Answer

This is not depended on the Labels you are using, the workflow is the same for every kind of view: The Keyboard appears and the textField should move up so the keyboard does not obscure the textField.

To achieve this, please have a look to this SO answer: Move textfield when keyboard appears swift .

I don't want to copy the whole code in there, just point you into the right direction.

Happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM