-1

I have a custom UIView that is a subview on a UIViewController.

I have it added in my storyboard and set it to Hidden.

My subview is also within another UIView that I'm using as a 'blur view' which is also initially Hidden.

I have functions that will unhide & hide the subviews.

My custom subview has a UITextField. I can show the keyboard and move the subview up with no problems. When I type in the keyboard or dismiss it my subview moves up and to the left. When I try to show my subview again it shows incorrectly (up and to the left).

The custom subview starts at the center of my screen.

The goal is move it up when the keyboard shows so it will not cover the subview or the UITextField, allow the user to type in the UITextField, and then dismiss the keyboard and move the custom subview back to the center.

In my UIViewController:

// Showing the custom sub view
func displayCustomSubView() {
    if let window = UIApplication.shared.keyWindow {
        self.blurView.isHidden = false
        self.customSubView.isHidden = false
        self.blurView.frame = window.frame
        self.customSubView.center = window.center
        window.addSubview(self.blurView)
        UIApplication.shared.keyWindow?.bringSubviewToFront(self.blurView)
    }
}


// Hiding the custom sub view
// the custom sub view has a button I tap to hide
@objc func dismissCustomSubView() {
    self.blurView.isHidden = true
    self.customSubView.isHidden = true
}

// Show Keyboard
// Since I am using the window to make sure my blur view expands to the full frame, I have tried just moving the window up
@objc func keyboardWillShow(sender: NSNotification) {
    if let window = UIApplication.shared.keyWindow {
        window.frame.origin.y = -75
    }
}

// Hide Keyboard
@objc func keyboardWillHide(sender: NSNotification) {
    if let window = UIApplication.shared.keyWindow {
        window.frame.origin.y = 0
    }
}


// Custom Subview Extension

extension CustomSubView: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}

Added the Custom Subview Extension above.

Luke Irvin
  • 1,179
  • 1
  • 20
  • 39

1 Answers1

0

First add this notification within your viewDidLoad(). And make a global variable called var keyboardH: CGFloat = 0:

  NotificationCenter.default.addObserver(
    self,
    selector: #selector(keyboardWillShow),
    name: UIResponder.keyboardWillShowNotification,
    object: nil
)

And this function below:

@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height
    self.keyboardH = keyboardHeight
}

This function is called every time the keyboard is present and will reveal the keyboard height and later we can use this variable.

So in your code:

@objc func keyboardWillShow(sender: NSNotification) {
if let window = UIApplication.shared.keyWindow {
    let position = window.frame.origin.y - keyboardH
    window.frame.origin.y = position
     }
}
excitedmicrobe
  • 2,338
  • 1
  • 14
  • 30
  • I can show the keyboard no problem, it's getting my subview to relocate after dismissing the keyboard & keeping the screen the same when typing. – Luke Irvin Mar 01 '19 at 19:14