Answer the question
In order to leave comments, you need to log in
How to return screen size after scrollview scroll?
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*/
Answer the question
In order to leave comments, you need to log in
Decision:
@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
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question