@objc protocol KeyboardConstraintListener: class
{
func didOpenKeyboard()
func didCloseKeyboard()
}
class KeyboardConstraint: NSLayoutConstraint
{
@IBOutlet
weak var listener: KeyboardConstraintListener?
@IBInspectable
var skipAutoCalculateMarginToBottom: Bool = false
@IBInspectable
var keepMarginWhenOpen: Bool = false
private var originalConstant: CGFloat = 0.0
override func awakeFromNib()
{
super.awakeFromNib()
self.originalConstant = self.constant
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
deinit
{
NotificationCenter.default.removeObserver(self)
}
@objc func keyboardWillShow(notification: Notification)
{
self.listener?.didOpenKeyboard()
self.updateConstant(notification: notification, showing: true)
}
@objc func keyboardWillHide(notification: Notification)
{
self.listener?.didCloseKeyboard()
self.updateConstant(notification: notification, showing: false)
}
private func updateConstant(notification: Notification, showing: Bool)
{
let duration: TimeInterval = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double) ?? 0.2
guard let endFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else
{
return
}
guard let firstView: UIView = self.getView(item: self.firstItem) else
{
return
}
guard let secondView: UIView = self.getView(item: self.secondItem) else
{
return
}
guard let superview: UIView = firstView.superview else
{
return
}
let keyboardHeight: CGFloat = (showing ? max(endFrame.size.height - self.modifier(view: secondView), 0.0) : 0.0)
CATransaction.begin()
CATransaction.setDisableActions(true)
superview.layoutIfNeeded()
CATransaction.commit()
UIView.animate(
withDuration: duration,
delay: 0,
options: [.curveLinear],
animations:
{
self.constant = keyboardHeight + self.originalConstant
superview.layoutIfNeeded()
},
completion: nil)
}
private func modifier(view: UIView) -> CGFloat
{
guard let window: UIWindow = view.window else
{
return 0.0
}
guard let superview: UIView = view.superview else
{
return 0.0
}
guard !self.skipAutoCalculateMarginToBottom else
{
return 0.0
}
let origin: CGPoint = superview.convert(view.frame.origin, to: nil)
let bottom = origin.y + view.bounds.size.height
let expand = window.bounds.size.height - bottom
return expand - (self.constant - self.originalConstant) - (self.keepMarginWhenOpen ? self.originalConstant : 0.0)
}
private func getView(item: AnyObject?) -> UIView?
{
if let view = item as? UIView
{
return view
}
else if let view = (item as? UILayoutGuide)?.owningView
{
return view
}
return nil
}
}
Создайте ограничение так, чтобы textView.bottom = safeArea.bottom