在快速约束下编辑多个文本字段时移动键盘

问题描述 投票:0回答:1

我有UIViewController,看起来像这样:

enter image description here

我设置了所有相关的constraints

[我试图在键盘出现时向上移动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)
    }
}

我面临的问题是-

when I click the first <code>UITextField</code>, the <code>UIView</code> shifts nicely:

但是,当我单击第二个时,它再次移动,现在看起来像这样:

enter image description here

我注意到,如果删除约束,问题就消失了,但是,我确实需要使用约束。

所以,我在这里想念什么?

swift uitextfield constraints uiresponder
1个回答
0
投票

您必须将Scrollview用于uiview控制器,然后才能使用通知来调整uiview约束。

您的UIViewController层次结构应类似于此

ContainerView-> ScrollView->内容视图->现在就是您的视图。

我建议您将pod'TPKeyboardAvoiding'用于滚动视图中的未来动画,因为对于每个控制器,您都不想将通知放置到移动UIView的位置。

这里是UIView转换https://github.com/Vasu05/ScrollViewExample的链接演示>

© www.soinside.com 2019 - 2024. All rights reserved.