Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AlertView offset is not restored after dismissing keyboard #265

Closed
buganini opened this issue Mar 2, 2018 · 10 comments
Closed

AlertView offset is not restored after dismissing keyboard #265

buganini opened this issue Mar 2, 2018 · 10 comments
Labels

Comments

@buganini
Copy link

buganini commented Mar 2, 2018

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
Copy link
Owner

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

@cdinarte
Copy link

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
Copy link
Owner

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
Copy link

Not working on my side.

@sberrevoets
Copy link
Owner

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

@x10geeky
Copy link

x10geeky commented Dec 22, 2018

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
Copy link

piotrros commented Feb 9, 2019

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
Copy link
Owner

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
Copy link

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

@brbsBruno
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants