这是我想要实现的目标:
我有一种形式,在UISCrollView中封装了多个textField。开始编辑字段时,目标是将其设置在键盘上方。
为了实现这一目标:
问题是: 设置contentInset会触发带有延迟的contentOffset自动调整。
问题是: 如何禁用此自动行为
internal func keyboardWillShow(notification: Notification)
{
let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
self.scrollView.contentInset = UIEdgeInsetsMake(self.scrollView.contentInset.top,
self.scrollView.contentInset.left,
targetFrame.size.height,
self.scrollView.contentInset.right)
let apDelegate = UIApplication.shared.delegate as! AppDelegate
let absRect = self.currentSelectedField!.superview!.convert(self.currentSelectedField!.frame, to: apDelegate.window)
if absRect.origin.y + absRect.size.height > targetFrame.origin.y
{
let diff = (absRect.origin.y + absRect.size.height) - targetFrame.origin.y
UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions(rawValue: curve), animations: {
self.scrollView.contentOffset = CGPoint(x: self.scrollView.contentOffset.x,
y: self.scrollView.contentOffset.y + diff)
}, completion: { (done: Bool) in
})
}
}