0

I'm looking for some help with a UI related issue that I'm stumped with. I have a storyboard in my application that has a specific scene that looks like this:

enter image description here

The image above is where I've hacked the code so that it doesn't shift the view up when the keyboard comes up. But, as you can see, it's cutting off some of the objects in the view at the bottom.

I'm hoping to be able to slide all of the view objects up when the keyboard comes up, but keep the red banner stuck at the top and have it hover on top of the other items.

Here's the relevant code where the class responds to the keyboard being shown/hidden. This is not code I wrote as I'm working with a brand new codebase.

@objc
    func keyboardWillShow(note: Notification) {
        //print("**** EnterPasscodeViewController -> keyboardWillShow...")
        if let userInfo = note.userInfo,
            let keyboardSize = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            //Taking space from origin to move as this view is already drawn with layouts and completely visible to user.
            let viewItemsHeight = forgotPasscodeButton.frame.origin.y
            //Adding some buffer becasue of devices like iphone 5
            let deltaY = view.frame.height - viewItemsHeight - keyboardSize.height + 24.0
            
            if let navigationControllerBar = self.navigationController?.navigationBar {
                let excessHeightAboveView = navigationControllerBar.frame.height + UIApplication.shared.statusBarFrame.height
                view.frame.origin.y = deltaY > excessHeightAboveView ? excessHeightAboveView : deltaY
                print("**** EnterPasscodeViewController -> keyboardWillShow -> new y position is \(self.view.frame.origin.y)")
            }
        }
    }
    
    @objc
    func keyboardWillHide(note: Notification) {
        //print("**** EnterPasscodeViewController -> keyboardWillHide...")
        guard let height = self.navigationController?.navigationBar.frame.height else { return }
        self.view.frame.origin.y = height + UIApplication.shared.statusBarFrame.height
        print("**** EnterPasscodeViewController -> keyboardWillHide -> new y position is \(self.view.frame.origin.y)")
    }
    
    deinit {
        unsubscribeToKeyboardNotifications()
    }
    
    func subscribeToKeyboardNotifications() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(keyboardWillShow(note:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(keyboardWillHide(note:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    func unsubscribeToKeyboardNotifications() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
        notificationCenter.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
    }

Here is an image showing the storyboard's scene layout:

enter image description here

The "Error View" object in the scene is the red banner.

The ViewController is called EnterPasscodeViewController and subclasses UIViewController:

public final class EnterPasscodeViewController: UIViewController {

}

So, how can I float the red banner on top of the rest of the content when the keyboard shifts the view upwards?

JFortYork
  • 105
  • 8

0 Answers0