Reputation: 490
I'm developing an iOS App that uses a PageViewController to wrap several "Chat rooms". I've a TextField in each page and when I swipe in order to move to another page I wanna change the focus from the current page TextField to the new page TextField without losing the keyboard.
I've tried different approaches but I cannot do that, every time I call becomeFirstResponder in the new page TextField the keyboard goes down, then goes up immediately and then the textfields becomes the first responder.
Can you help me?
Thank you!
Upvotes: 0
Views: 1978
Reputation: 21891
Put the text field on the page view controller's view, instead of on each child page. From the page view controller, call self.view.bringSubviewToFront(textField)
to put it on top.
I just tested this in a new project (using the "Page-Based Application" template) and it works fine.
Upvotes: 1
Reputation: 490
I was able to find a solution for this problem:
Using a variable in each page
var firstResponderIsLocked: Bool = false
To check if the TextField should end editing
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
return !firstResponderIsLocked
}
And handling it when I swipe between pages:
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
if let viewControllerToShow = pendingViewControllers[0] as? LiveMessagingPageContentViewController {
pendingViewControllerIndex = viewControllerToShow.index
lockFirstResponder(pendingViewControllerIndex!)
}
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed && pendingViewControllerIndex != nil {
takeFirstResponder(pendingViewControllerIndex!)
}
pendingViewControllerIndex = nil
}
I make my app work as needed.
Upvotes: 2