3
override func viewDidLoad() {
    super.viewDidLoad()
    self.submitBtn.translatesAutoresizingMaskIntoConstraints = true

    emailTf.text = emailID

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    // Do any additional setup after loading the view.
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(PMSignupViewController.dismissKeyboard))
    tap.cancelsTouchesInView = false
    self.view.addGestureRecognizer(tap)
    // Do any additional setup after loading the view.
}

@objc func dismissKeyboard()
{
    view.endEditing(true)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@objc func keyboardWillShow(notification: NSNotification) {
    print("keyboardWillShow")
    let userInfo:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height
    self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: true)
}

@objc func keyboardWillHide(notification: NSNotification){
    print("keyboardWillHide")
    let userInfo:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height
    self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: false)

}
func moveButton(_ button: UIButton, moveDistance: Int, up: Bool) {
    let moveDuration = 0.3
    let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)

    UIView.beginAnimations("animateTextField", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(moveDuration)
    self.submitBtn.frame = self.submitBtn.frame.offsetBy(dx: 0, dy: movement)
    UIView.commitAnimations()
}

@IBAction func submitBtnAction(_ sender: Any) {

}



}

So basically I have a view with a textfield on top and a button in the bottom. When my keyboard is up the button moves up along with it. But the button doesn't trigger the action method while it is on top of the keyboard. But when the keyboard is dismissed and its back to the bottom of the screen it works fine. Any input on this?

iOS Developer
  • 538
  • 4
  • 10
sibin
  • 47
  • 9
  • try to `view.bringSubview(toFront: submitBtn)` call when moving up, just to be sure that it's not overlayed by some other (possibly) invisible view – Milan Nosáľ Feb 28 '18 at 09:40
  • 1
    Can you check if your UIButton is inside its superview or not. UIbutton won't repond to touches if it is outside its superview bounds. – iOS Developer Feb 28 '18 at 09:41
  • @ Milan Nosáľ tried ...but no luck :( – sibin Feb 28 '18 at 09:44
  • are you using autolayout or setting frames directly? and did you confirm that the button.frame is in its superview frame after animation? – Milan Nosáľ Feb 28 '18 at 09:51
  • i tried both frame and constraint methods .. i did call view.bringSubview(toFront: submitBtn) but no luck – sibin Feb 28 '18 at 09:52
  • but, did you confirm that the button is still inside its superview? – Milan Nosáľ Feb 28 '18 at 09:57
  • use a view hierarchy debugger to capture the screen when the keyboard is up and check it out – Milan Nosáľ Feb 28 '18 at 09:57
  • and would recommend to add the rest of the code of the mentioned view controller, because obviously the problem lies somewhere there – Milan Nosáľ Feb 28 '18 at 10:02
  • > Printing description of $37: would this help? – sibin Feb 28 '18 at 10:05
  • is it inside of its superview? – Milan Nosáľ Feb 28 '18 at 10:09
  • yes it is inside the superview – sibin Feb 28 '18 at 10:19
  • ok, let's do some experimenting: 1. try to remove animation from the moving up and set just the frame directly - let us make sure that animation is not messing something up; 2. I would not expect `userInteractionEnabled = NO` - try setting `submitBtn.isUserInteractionEnabled = true` explicitly after changing its frame; – Milan Nosáľ Feb 28 '18 at 10:23
  • if that does not move us anywhere, I am going to need to ask you to share the rest of the code.. ideally there would be a minimal runnable code that can be used to replicate the issue – Milan Nosáľ Feb 28 '18 at 10:24
  • finally if found whats causing it .. i commented this line "tap.cancelsTouchesInView = false" in view did now it works perfectly fine :D... But i still dont understand why is that – sibin Feb 28 '18 at 10:28

2 Answers2

1

You better use auto layout for that as frame-layout glitches everything when changed so, hook bottom constraint of the button as IBOutlet names say buttonBotCon

 @objc func keyboardWillShow(notification: NSNotification) {
        print("keyboardWillShow")
        let userInfo:NSDictionary = notification.userInfo! as NSDictionary
        let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
        self.buttonBotCon.constant = -keyboardHeight
        self.view.layoutIfNeeded()
    }

    @objc func keyboardWillHide(notification: NSNotification){
        print("keyboardWillHide")
       self.buttonBotCon.constant = 0
        self.view.layoutIfNeeded()

    }

Edit : see demo here that accomplish the same thing buttonUpKeyboard

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Finally i figured out whats causing the issue

i just had to comment out tap.cancelsTouchesInView = false this line in viewdid load method...and works perfectly fine..but i dont know why is that .

sibin
  • 47
  • 9