0

In the app I am working on I have two view controllers inside a scroll view. The second view controller (VC2) contains a text view. You can see the setup on the image below:

my setup!

When I scroll from VC2 to VC1, the keyboard persists and covers the content of VC1. I managed to solve the problem by making the scroll view the first responder on scrollViewDidScroll event. This works, but it results in the keyboard disappearing even on a partial scroll, which can be annoying to the users. I can solve this problem by also checking the content offset but it strikes me as overcomplicated and not elegant at all. Is there a better way to do this?

Edit:

As Chonch and latenitecoder suggested, I detected the page change. I adapted the code from: Detecting UIScrollView page change to swift. Here it is:

var previousPage = 0

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    let pageWidth = scrollView.frame.size.width
    let fractionalPage = scrollView.contentOffset.x / pageWidth
    let page = Int(round(fractionalPage))
    if (previousPage != page) {
        // Page has changed, do your thing!
        self.becomeFirstResponder()
        // Finally, update previous page
        previousPage = page
    }
}
Community
  • 1
  • 1
user2634633
  • 509
  • 8
  • 21
  • It's not that hard just a bit of thought and approach When exactly do you want the keyboard to close? At what point should it dismiss? – latenitecoder Aug 25 '15 at 13:39
  • My scroll view has `pagingEnabled = true` . I'd like the keyboard to disappear when the scroll view snaps from the page containing VC 2 to the page containing VC 1. I should have added that I am planning to add a VC 3, so I am looking for a method that will work regardless of the number of view controllers. – user2634633 Aug 25 '15 at 13:43
  • 1
    "I'd like the keyboard to disappear when the scroll view snaps from the page containing VC 2 to the page containing VC 1" - theres your answer - using the scrollview delegate methods, your approach will be to detect when a scrollview has stopped moving and currently not displaying VC2 to dismiss keyboard (and plenty of examples on SO on how to do this). – latenitecoder Aug 25 '15 at 13:53
  • Thanks, I did that and it worked – user2634633 Aug 25 '15 at 14:12

2 Answers2

0

You can set the UIScrollView's pagingEnabled property to YES and only call resignFirstResponder for the UITextView when the paging ended and the resulting page is VC1. As long as the current page remains VC2, you don't call resignFirstResponder, and the keyboard remains shown.

However, notice that it may actually be a good idea to hide the keyboard as soon as the user starts scrolling (as you're describing is your current state). Maybe you should leave it like this and when the paging ends, check if the current page is VC2, and if it is, call becomeFirstResponder on the UITextView in order to display the keyboard again.

Rony Rozen
  • 3,957
  • 4
  • 24
  • 46
0

I'd suggested to use UIPageViewController to horizontal scrolling controllers. Then you can do the same action in it's delegate method - (void)pageViewController: didFinishAnimating: previousViewControllers: transitionCompleted: but it will save you memory a lot.

Polina
  • 273
  • 4
  • 13