SDCAlertView icon indicating copy to clipboard operation
SDCAlertView copied to clipboard

AlertView offset is not restored after dismissing keyboard

Open buganini opened this issue 6 years ago • 10 comments

I am using

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
alert.contentView.addGestureRecognizer(tap)

and

@objc func dismissKeyboard() {
    alert.contentView.endEditing(true)
}

to dismiss keyboard when blank area is tapped, the alertview stay at the position where it was pushed to by keyboard.

buganini avatar Mar 02 '18 16:03 buganini

Thanks for reporting, I'll try to look at this soon.

sberrevoets avatar Mar 03 '18 08:03 sberrevoets

Fixed it temporarily by saving the Y offset for the popup view, when the keyboard shows/hides, and then setting the y offset on these events.

cdinarte avatar May 30 '18 13:05 cdinarte

I think this is fixed on https://github.com/sberrevoets/SDCAlertView/tree/handle-keyboard-dismiss.

Can you check it out and see if this solves your problem?

sberrevoets avatar Jun 10 '18 01:06 sberrevoets

Not working on my side.

mureatencio avatar Aug 22 '18 08:08 mureatencio

@mureatencio on the branch I provided above? I was able to reproduce the issue, but not anymore on that branch.

sberrevoets avatar Sep 02 '18 04:09 sberrevoets

In function listenForKeyboardChanges I replace function with

   private func listenForKeyboardChanges() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
    }

and add two function with

    @objc private func keyboardWillShow(notification: NSNotification) {
        let newFrameValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue
        guard let newFrame = newFrameValue?.cgRectValue else {
            return
        }
        
        self.verticalCenter?.constant = -newFrame.height / 2
        self.alert.layoutIfNeeded()
    }
    
    @objc private func keyboardWillHide(notification: NSNotification){
        self.verticalCenter?.constant = 0
        self.alert.layoutIfNeeded()
    }

x10geeky avatar Dec 22 '18 19:12 x10geeky

Did you change master or handle-keyboard-dismiss branch? I've tried both and it doesn't work on master and on handle-keyboard-dismiss I'm getting crash on device when accessing alert.contentView (just after AlertController creation).

piotrros avatar Feb 09 '19 11:02 piotrros

I'm not aware of any changes that would cause a crash, though the other branch is obviously a bit outdated at this point.

sberrevoets avatar Feb 11 '19 01:02 sberrevoets

Can you tell me how to implement this keyboard dismiss fix on master branch?

piotrros avatar Feb 11 '19 08:02 piotrros

The change above suggested by @x10geeky worked fine for me. 👏

@twostraws in here suggested that keyboardWillChangeFrameNotification isn't enough to catch scenarios with a hardware keyboard being connected.

Any chance to get that on the master branch?

brbsBruno avatar Jan 28 '20 17:01 brbsBruno