sberrevoets / SDCAlertView

The little alert that could
MIT License
1.86k stars 298 forks source link

AlertView offset is not restored after dismissing keyboard #265

Closed buganini closed 1 month ago

buganini commented 6 years ago

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.

sberrevoets commented 6 years ago

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

cdinarte commented 6 years ago

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.

sberrevoets commented 6 years ago

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?

mureatencio commented 6 years ago

Not working on my side.

sberrevoets commented 6 years ago

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

x10geeky commented 5 years ago

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()
    }
piotrros commented 5 years ago

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).

sberrevoets commented 5 years ago

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

piotrros commented 5 years ago

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

brbsBruno commented 4 years ago

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?