为什么出现键盘时 scrollrectToVisible 不起作用?

时间:2021-07-13 09:31:25

标签: ios swift

在代码中,我有类似这样的东西,当键盘确实出现在屏幕上时会调用它。

scrollView.scrollRectToVisible(blikCodeView.frame, animated: true)

尽管 blikCodeView 根本不可见(因为它被当前显示的键盘覆盖),但它不会向上滚动以使其可见。为什么?

1 个答案:

答案 0 :(得分:0)

试试这个

    let rScreenHeight = UIScreen.main.bounds.height - 260//keyboard height as 8 & SE
    let newY = rScreenHeight - txt.frame.height - 16
    let y = scrollView.height - newY
    let newOffSet = CGPoint(x: 0, y: y)
    scrollView.setContentOffset(newOffSet, animated: true)

注意:这里我设置了 txtField.autocorectionType = .no