簡體   English   中英

當我更新contentInset時,UIScrollView會更改contentOffset

[英]UIScrollView changes contentOffset when I update contentInset

UIScrollView固定在控制器的視圖邊緣。

UIKeyboardWillShow我通過增加鍵盤高度來增加contentInsets底值。 動畫制作完成后,內容將沿向上方向稍微滾動。

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0.3
    self.formContainer.contentInset = UIEdgeInsets(top: self.rowPadding, left: 0, bottom: keyboardSize.height + self.navigator.frame.height + 10, right: 0)
    self.navigatorBottom.constant = -keyboardSize.height
    UIView.animate(withDuration: duration) {
        self.view.layoutIfNeeded()
    }
}

無論如何,這里有禁止內容滾動的功能嗎?

在開始動畫之前保存UIScrollView contentOffset並將動畫completionHandler保存值設置為contentOffset

看起來像這樣:

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0.3
    let currentContentOffset = self.formContainer.contentOffset.y //assume your scrollView scrolls vertically
    self.formContainer.contentInset = UIEdgeInsets(top: self.rowPadding, left: 0, bottom: keyboardSize.height + self.navigator.frame.height + 10, right: 0)
    self.navigatorBottom.constant = -keyboardSize.height
    UIView.animate(withDuration: duration, animations: {
        self.view.layoutIfNeeded()
    }) { _ in
        self.formContainer.contentOffset.y = currentContentOffset
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM