这就是我的故事板目前的样子: onboardingVC 包含嵌入 PageViewController 的 UIView容器,其中包含对下一个3个ViewControllers的引用< / p>
在3个ViewControllers(电子邮件,密码,昵称)中,我有一个文本字段,在ViewDidLoad()的所有3个视图控制器中,我调用textField.becomeFirstResponder()
override func viewDidLoad() {
super.viewDidLoad()
textField.becomeFirstResponder()
}
现在键盘只显示在EmailVC上(PageViewController中的第一页)。
但是,当我点击“下一步”按钮并转到下两个视图控制器时,键盘会显示一毫秒并立即解除。
所以,我的问题是如何使键盘保持不变,显示并专注于所有三个文本字段
class OnBoardingViewController: UIViewController {
weak var delegate: NextPageDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func nextPage(_ sender: UIButton) {
delegate?.showNextPage()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Const.Segue.embeded_email_registration {
delegate = segue.destination as? PageViewController
}
}
@IBAction func dismissVC(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
protocol NextPageDelegate: class {
func showNextPage()
}
class PageViewController: UIPageViewController {
fileprivate lazy var pages: [UIViewController] = {
return [
self.getViewController(withIdentifier: Const.EmailRegistration.EmailVC),
self.getViewController(withIdentifier: Const.EmailRegistration.PasswordVC),
self.getViewController(withIdentifier: Const.EmailRegistration.NicknameVC),
self.getViewController(withIdentifier: Const.EmailRegistration.BirthdateVC)
]
}()
fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController {
return UIStoryboard(name: "Login", bundle: nil).instantiateViewController(withIdentifier: identifier)
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let firstVC = pages.first {
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
}
removeSwipeGesture()
}
func removeSwipeGesture() {
for view in self.view.subviews {
if let subView = view as? UIScrollView {
subView.isScrollEnabled = false
}
}
}
}
扩展名PageViewController:UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let previousIndex = currentIndex - 1
return (previousIndex == -1) ? nil : pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.index(of: viewController)!
let nextIndex = currentIndex + 1
return (nextIndex == pages.count) ? nil : pages[nextIndex]
}
}
extension PageViewController: NextPageDelegate {
func showNextPage() {
if let currentViewController = viewControllers?[0] {
if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) {
setViewControllers([nextPage], direction: .forward, animated: true, completion: nil)
}
}
}
}
class EmailInputViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//textField.becomeFirstResponder()
}
override func viewWillAppear(_ animated: Bool) {
textField.becomeFirstResponder()
}
}
EmailInputViewController,PasswordInputViewController和NicknameInputViewController具有相同的代码
答案 0 :(得分:1)
我准备了一个演示,请看看
<强> PageviewController 强>
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pages = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
// Do any additional setup after loading the view.
let first = storyboard?.instantiateViewController(withIdentifier: "first") as! FirstViewController
let second = storyboard?.instantiateViewController(withIdentifier: "second") as! SecondViewController
pages.append(first)
pages.append(second)
setViewControllers([first], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
self.addChildViewController(first)
self.addChildViewController(second)
self.didMove(toParentViewController: first)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex: Int = pages.index(of: viewController)!
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex: Int = pages.index(of: viewController)!
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return pages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
持有PageViewController的容器视图控制器
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var pageVC: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonAction(_ sender: Any) {
if let page = self.childViewControllers.first as? PageViewController {
let second = storyboard?.instantiateViewController(withIdentifier: "second") as! SecondViewController
page.setViewControllers([second], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
}
}
这是我的FirstViewController,它是PageViewController的第一页
import UIKit
class FirstViewController: UIViewController {
@IBOutlet weak var firstText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
firstText.becomeFirstResponder()
}
}
<强> SecondViewController 强>
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var secondText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
secondText.becomeFirstResponder()
}
}
在此演示键盘中,当用户点击下一个按钮
时,键盘不会停止