1

This is the code i wrote to move the button up when the keyboard is enabled:

@IBOutlet var lastNameText: HoshiTextField! 
@IBOutlet var firstNameText: HoshiTextField!

override func viewDidLoad() {      
        var fab = MDCFloatingButton()
        fab.translatesAutoresizingMaskIntoConstraints = false
        fab.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant:-30.0).isActive = true
        fab.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant:-240.0).isActive = true
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:.UIKeyboardWillShow, object: nil )
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:.UIKeyboardWillHide, object: nil)

        @objc func keyboardWillShow(notificaion: NSNotification) {
            if let keyboardSize = (notificaion.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue{
                //fab = UIButton(frame: CGRect(10+20, self.view.frame.size.height - keyboardSize.height - 40,80,30))     
                fab.frame = CGRect(x: 320, y: 350, width: 60, height: 60)    
        }       
 }

@objc func keyboardWillHide(notification: NSNotification ) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue{
        fab.frame = CGRect(x: 320, y: 350, width: 65, height: 65)
    }
}

Please give input.

Mina
  • 2,167
  • 2
  • 25
  • 32
  • 3
    Possible duplicate of [How to make a UITextField move up when keyboard is present?](https://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Prashant Tukadiya Feb 13 '18 at 05:24
  • When i click the first text field button moved up and when i click the second text field it goes to its initial position.....i want button to go to its initial position only when keyboard gets hide –  Feb 13 '18 at 05:26
  • What's the point of changing button's height? Make a good comparison on fab.frame = CGRect(x: 320, y: 350, width: 60, height: 60) and fab.frame = CGRect(x: 320, y: 350, width: 65, height: 65) for yourself. – El Tomato Feb 13 '18 at 06:29

3 Answers3

3

Updated to Swift 4.+

viewDidLoad

//  Moving next button when keyboard appears
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name:UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name:UIResponder.keyboardWillHideNotification, object: nil)

When showing the keyboard:

@objc func keyboardWillShow(notification:NSNotification){
    let userInfo = notification.userInfo!
    var keyboardFrame:CGRect = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)
    self.buttonNextBottomConstraint.constant = keyboardFrame.size.height
}

When hiding the keyboard:

@objc func keyboardWillHide(notification:NSNotification){
    self.buttonNextBottomConstraint.constant = 0
}
ytpm
  • 4,962
  • 6
  • 56
  • 113
1

you need add bottom constraint for your button, and change constant of this constraint programmatically. First you need to add Observers to viewDidLoad() method.

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)

And add selectors methods:

func keyboardWillShow(notification:NSNotification){
    var userInfo = notification.userInfo!
    var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)

    bottom.constant = keyboardFrame.size.height
}

func keyboardWillHide(notification:NSNotification){
    bottom.constant = 0
}
Mixon McLaren
  • 257
  • 2
  • 6
1

First add the textfield delegate method in your class after that simply put this code and change it according to you :-

func textFieldDidBeginEditing(_ textField: UITextField) {
    switch textField {
    case txtFldSponsorID:
        moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
    case txtFldFullName:
        moveTextfield(textfield: txtFldFullName, moveDistance: -10, up: true)
    case txtFldEmail:
        moveTextfield(textfield: txtFldEmail, moveDistance: -10, up: true)
    case txtFldMobile:
        moveTextfield(textfield: txtFldMobile, moveDistance: -10, up: true)
    case txtFldAddress:
        moveTextfield(textfield: txtFldAddress, moveDistance: -80, up: true)
    case txtFldCity:
        moveTextfield(textfield: txtFldCity, moveDistance: -80, up: true)
    default:
        break
    }
}

func textFieldDidEndEditing(_ textField: UITextField) {
    switch textField {
    case txtFldSponsorID:
        moveTextfield(textfield: txtFldSponsorID, moveDistance: 0, up: true)
    case txtFldFullName:
        moveTextfield(textfield: txtFldFullName, moveDistance: 10, up: true)
    case txtFldEmail:
        moveTextfield(textfield: txtFldEmail, moveDistance: 10, up: true)
    case txtFldMobile:
        moveTextfield(textfield: txtFldMobile, moveDistance: 10, up: true)
    case txtFldAddress:
        moveTextfield(textfield: txtFldAddress, moveDistance: 80, up: true)
    case txtFldCity:
        moveTextfield(textfield: txtFldCity, moveDistance: 80, up: true)
    default:
        break
    }
}