U
U
uzolenta2019-03-31 16:44:03
iOS
uzolenta, 2019-03-31 16:44:03

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*/

This is how I move the content when opening the keyboard - everything works.
But after closing the keyboard, space is added below the initial screen, on which there is no longer a ScrollView. How to get rid of the added size (red square)?
When you scroll down (pull the screen down), the field returns to its original place and the scroll works in full screen, and when you scroll up, there is free space and the scroll is not full screen (screenshot).
The latest screenshot is how the screen looks when you open the application, i.e. the scroll is not much larger than the phone screen, the field is at the bottom of the scroll.
UPD. The first time you press it, the keyboard is hidden, but an additional screen remains, when you press the screen again, this additional screen is removed. Why doesn't it work on the first press to hide the keyboard and remove extra space on the screen?
5ca0c40990e16008479345.jpeg5ca0fe7ab6aa3923807390.jpeg5ca0fe7ab74f7884368966.jpeg5ca0fe7aaea42692192648.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uzolenta, 2019-04-01
@uzolenta

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
    }

Wherever you need to call:
Shift ScrollView works, everything returns back.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question