How to return the size of the screen after scrolling the scroll view?

override func viewDidLoad() {
super.viewDidLoad()

 NotificationCenter.default.addObserver(self, selector: #selector(KeyBoard), name: UIResponder.keyboardDidHideNotification, object: nil)
 NotificationCenter.default.addObserver(self, selector: #selector(KeyBoard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

}

 @objc func KeyBoard(notification: Notification){
 let userInfo = notification.userInfo!
 let keyBoardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
 let keyBoardViewEndFrame = view.convert(keyBoardScreenEndFrame, from: view.window)

 if notification.name == UIResponder.keyboardWillHideNotification{
 print("HIDE KEYBOARD")
 scrollView.contentInset = UIEdgeInsets.zero
 } else {
 scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyBoardViewEndFrame.height, right: 0) 
}
 scrollView.scrollIndicatorInsets = scrollView.contentInset
}


 /*Close keyboard*/
 @IBAction func hideKeyBoard(_ sender: UITapGestureRecognizer) {
view.endEditing(true)
 scrollView.contentInset = UIEdgeInsets.zero
 print("HIDE KEYBOARD2")
}
 /*END Close keyboard*/


So move the content when you open the keyboard - everything works.
But after closing the keyboard adds a space below the initial screen on which there is no ScrollView. How to get rid of the added size (the red square)?
When scrolling down (pull the screen down), the field stands in its original location and scrolling works on the whole screen, and when scrolling up, then there is a free place and not scrolling the whole screen (screenshot).
The last screenshot is how the screen looks when the application is opened, ie scrolling slightly larger phone screen, scroll box is at the bottom.

UPD. The first press hides keyboard, but remains a secondary screen, when pressing on the screen is removed, this additional screen. Why not work on the first press hides keyboard and removing unnecessary space on the screen?
5ca0c40990e16008479345.jpeg5ca0fe7ab6aa3923807390.jpeg5ca0fe7ab74f7884368966.jpeg5ca0fe7aaea42692192648.jpeg
March 19th 20 at 09:25
1 answer
March 19th 20 at 09:27
Solution
Solution:
@IBOutlet weak var scrollView: UIScrollView!

 override func viewDidLoad() {
super.viewDidLoad()

 NotificationCenter.default.addObserver(self, selector: #selector(KeyBoard), name: UIResponder.keyboardDidHideNotification, object: nil)
 NotificationCenter.default.addObserver(self, selector: #selector(KeyBoard), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

}


 @objc func KeyBoard(notification: Notification){
 let userInfo = notification.userInfo!
 let keyBoardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
 let keyBoardViewEndFrame = view.convert(keyBoardScreenEndFrame, from: view.window)

 if notification.name == UIResponder.keyboardDidHideNotification{
 scrollView.contentInset = UIEdgeInsets.zero
}
 if notification.name == UIResponder.keyboardWillChangeFrameNotification{
 scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyBoardViewEndFrame.height, right: 0)
}
 scrollView.scrollIndicatorInsets = scrollView.contentInset
 }


Where You need call:
view.endEditing(true)

Shift the ScrollView works, back it all comes back.

Find more questions by tags SwiftiOS