我有一个pageViewController,它有两个可以在它们之间滑动的子视图控制器。其中一个有一个textView,当你滚动到那个页面时我想成为第一个响应者,然后当你滚动时失去焦点。现在我有这个:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
composeTextView.delegate = self
composeTextView.becomeFirstResponder()
}
一旦开始滚动到视图,键盘就会显示。但是一旦滚动完成,键盘就会消失。
为什么会发生这种情况?
答案 0 :(得分:1)
我最终找到了这个问题的答案。所以,在viewDidAppear
我添加了这个:
DispatchQueue.main.async(execute: {() -> Void in
let strongSelf: TextPostViewController = self
strongSelf.composeTextView.becomeFirstResponder()
})
我很高兴给任何能解释其原因的人提供复选标记。
答案 1 :(得分:1)
它可以正常过渡样式的页面卷曲,但不适用于滚动。
我认为问题出在viewDidAppear
之后,旧页面立即辞去第一响应者,并选择了新页面,因此您设置为firstResponder
的内容都会丢失。这是由于旧视图和新视图在窗口中反复添加和删除而造成的,从而影响了响应者链。我认为,在旧视图从屏幕上滚动出来后,由于某种原因它会重新添加到窗口中,因为当您滚动到旧视图时,它就可以使用了。
正如您在此日志中所看到的,在viewDidAppear之后,两个视图的窗口都有许多奇怪的变化。新视图为0x7fd87fa24160,为什么为什么旧视图0x7fd87fa059a0被删除并多次添加到窗口中?
2020-06-01 10:22:14.093911+0100 Paging2[45575:3540067] DetailViewController 0x7fd87fa15850 viewDidAppear: 0x7fd87fa24160
2020-06-01 10:22:14.094973+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x0
2020-06-01 10:22:14.095358+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x0
2020-06-01 10:22:14.096090+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x7fd87f80f3d0
2020-06-01 10:22:14.096511+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x7fd87f80f3d0
2020-06-01 10:22:14.099019+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x0
2020-06-01 10:22:14.099318+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x0
2020-06-01 10:22:14.099812+0100 Paging2[45575:3540067] MyView 0x7fd87fa059a0 didMoveToWindow 0x7fd87f80f3d0
2020-06-01 10:22:14.100306+0100 Paging2[45575:3540067] MyView 0x7fd87fa24160 didMoveToWindow 0x7fd87f80f3d0
为了进行比较,这里是推送时的UINavigationController。我在此question中了解到,移至窗口是为了进行过渡,因此打开慢速动画会有所帮助:
2020-06-01 10:49:16.786872+0100 NavWindowTest[45792:3560379] View 0x7fbfba608090 didMoveToWindow 0x0 (current view removed from window)
2020-06-01 10:49:16.787170+0100 NavWindowTest[45792:3560379] View 0x7fbfba608090 didMoveToWindow 0x7fbfba50c150 (current view added to window for transition)
2020-06-01 10:49:16.787577+0100 NavWindowTest[45792:3560379] View 0x7fbfba5124d0 didMoveToWindow 0x7fbfba50c150 (new view added)
2020-06-01 10:49:21.801791+0100 NavWindowTest[45792:3560379] View 0x7fbfba608090 didMoveToWindow 0x0 (old view removed from transition)
2020-06-01 10:49:21.803690+0100 NavWindowTest[45792:3560379] ViewController 0x7fbfbd005dd0 viewDidAppear: 0x7fbfba5124d0
viewDidAppear
在导航时最后被调用但在分页中首先被调用的事实使我认为存在问题。