Swift 编辑具有约束的多个文本字段时移动键盘
我有Swift 编辑具有约束的多个文本字段时移动键盘,swift,uitextfield,constraints,uiresponder,Swift,Uitextfield,Constraints,Uiresponder,我有UIViewController,它看起来像这样: 我设置了所有相关的约束 当键盘显示时,我试图向上移动UIView——当我单击下面的UITextfields时 我有以下代码: static func addRemoveKeyboardObserver(addObserver: Bool, keyboardMargin: CGFloat, view: UIView?) { Utils.keyboardMargin = keyboardMargin Utils.heightC
UIViewController
,它看起来像这样:
我设置了所有相关的约束
当键盘显示时,我试图向上移动UIView
——当我单击下面的UITextfields
时
我有以下代码:
static func addRemoveKeyboardObserver(addObserver: Bool, keyboardMargin: CGFloat, view: UIView?)
{
Utils.keyboardMargin = keyboardMargin
Utils.heightChangingView = view
if addObserver
{
NotificationCenter.default.addObserver(self, selector: #selector(Utils.keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(Utils.keyboardWillChange(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(Utils.keyboardWillChange(notification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
}
else
{
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
Utils.keyboardHeight = 0
}
}
@objc static func keyboardWillChange(notification: Notification)
{
let userInfo = notification.userInfo!
let beginFrameValue = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)!
let beginFrame = beginFrameValue.cgRectValue
let viewShouldMove = notification.name == UIResponder.keyboardWillShowNotification || notification.name == UIResponder.keyboardWillChangeFrameNotification
if Utils.keyboardHeight == 0 { Utils.keyboardHeight = -beginFrame.height + Utils.keyboardMargin }
let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
if let view = Utils.heightChangingView
{
view.frame.origin.y = viewShouldMove ? Utils.keyboardHeight : 0
UIView.animate(withDuration: duration, delay: TimeInterval(0), options: animationCurve, animations: { view.layoutIfNeeded() }, completion: nil)
}
}
我面临的问题是——
但是,当我单击第二个按钮时,它再次移动,现在看起来是这样的:
我注意到,如果我删除约束,问题就会消失,但是,我确实需要使用约束
那么,我缺少什么呢?您必须为uiview控制器使用scrollview,然后才能使用通知来调整uiview约束 您的UIViewController层次结构应该如下所示 ContainerView-->滚动视图-->内容视图-->现在是您的视图 我建议您在scrollview中的未来动画中使用pod“TPKeyboardAvoiding”,因为对于每个控制器,您都不希望在切换视图时发出通知
以下是UIView的链接演示,用于移动现在您的视图的约束条件是什么。如何为ScrollView、ContentView提供约束我正在获取红色边框线设置容器内的ScrollView,将ScrollView端到端连接到容器,并将其宽度设置为containerview。现在添加contentview将顶部、左侧和右侧约束附加到scrollview,并将contentview宽度设置为等于scrollview。现在您必须为内容视图指定高度,并将底部附加到scrollview末端。希望这有帮助!