【问题标题】:Moving keyboard when editing multiple textfields with constraints swift快速编辑具有约束的多个文本字段时移动键盘
【发布时间】:2020-04-05 14:33:53
【问题描述】:

我有UIViewController,看起来像这样:

我设置了所有相关的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)
    }
}

我面临的问题是 -

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

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

那么,我在这里错过了什么?

【问题讨论】:

    标签: swift uitextfield constraints uiresponder


    【解决方案1】:

    您必须为您的 uiview 控制器使用滚动视图,然后您可以使用通知来调整您的 uiview 约束。

    你的 UIViewController 层次结构应该是这样的

    ContainerView --> ScrollView --> 内容视图 --> 现在你的视图。

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

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

    【讨论】:

    • ScrollView --> Content View --> Now Your View. 的约束条件是什么
    • 如何给scrollview、contentview赋予约束
    • 我的边框线变红了
    • 在容器内设置滚动视图,将滚动视图端到端附加到容器并将其宽度设置为 containerview 。现在添加 contentview attach top ,left , right 约束到滚动视图并将内容视图宽度设置为等于滚动视图。现在您必须为内容视图提供高度并将底部附加到 scrollview end 。希望这会有所帮助!
    猜你喜欢
    • 2017-01-29
    • 2014-10-30
    • 1970-01-01
    • 2018-06-15
    • 2012-12-07
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    相关资源
    最近更新 更多