当键盘置于其上方时,尝试获取UITextView来移动内容

时间:2018-09-26 01:41:44

标签: uiscrollview uitextfield uitextview swift4

我有一个UITextView

    @IBOutlet weak var note: UITextView!
note.delegate = self

文本视图位于UIScrolLView内部

@IBOutlet weak var scrollView: UIScrollView!

我想对此做出回应:

// move the scrollView when the keyboard is presented
    NotificationCenter.default.addObserver(self, selector: #selector(Keyboard), name: Notification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(Keyboard), name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)

在按下时调用此函数:

@objc func adjustForKeyboard(_ notification: Notification) {
    let userInfo = notification.userInfo!

    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
    let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)

    if notification.name == NSNotification.Name.UIKeyboardWillHide {
        scrollView.contentInset = UIEdgeInsets.zero
    } else {
        scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }
    scrollView.scrollIndicatorInsets = scrollView.contentInset
}

我想发生的是,当您点击UITextView时,如果键盘要隐藏文本视图,则内容将向上滚动,以使TextView始终可见。此方法对我的UITextFields很好,但是对UITextView不起作用。

按照本教程操作,仍然对我不起作用。我不确定Fields是否会在视频中使用UITextView或UITextFields。

OW TO AUTO ADJUST UISCROLLVIEW WHEN KEYBOARD IS UP (SWIFT-4, XCODE-9)

How to adjust a UIScrollView to fit the keyboard

0 个答案:

没有答案