我有一个UIPageViewController
与多个UIViewController
,每个包含UITextField
。
每当我滑动UIPageViewController
时,键盘自动解除。有没有办法防止这种行为?
在新的becomeFirstResponder
上调用UITextfield
,但动画不会解决问题。
您可以尝试将PageViewController嵌入为其他viewController的ChildViewController。
我相信这样,PageViewController中的导航不会影响键盘。我不确定是否需要它,但如果它仍然不起作用,您可以在发生转换时将ParentViewController设置为firstResponder。
遗憾的是,这似乎来自转换的动画,显示为已完成,在实际完成之前,我能想到的变通方法是1.使动画设置为假2.在下一个VC中设置textFieldShouldEndEditing以返回NO或使用bool 3.在动画中添加延迟,或者在下一个VC viewWillAppear中添加延迟
当在setViewControllers
上使用UIPageViewController
(设置没有滚动的控制器)时,似乎在scrollView
到达其最终位置之前调用完成块。当它发生时,它会解雇第一响应者。
我们找到的解决方案首先是抓住滚动视图:
// In UIPageViewController subclass
for subview in self.view.subviews {
if let scrollV = subview as? UIScrollView {
scrollV.delegate = self
self.scrollView = scrollV // Optional, we don't really need it
}
}
当它的offset
是中间控制器的x
位置时,滚动视图位置完成。也就是说,offset
将等于视图的长度。
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x == self.view.width {
// Transition is 'really' done.
}
}
此时,您可以发送转换完成的通知。我们所做的是保持一个完成块并在转换完成后调用它。
创建一个函数,以便控制器可以传入一个块:
func transitionCompleted(completed:(()->())?) {
self.transitionCompletedBlock = completed
}
转换完成后:
self.transitionCompletedBlock?()
带键盘的控制器看起来像:
pagerController.transitionCompleted(completed: {
self.textfield.becomeFirstResponder()
})