首页 > 解决方案 > 仅当键盘覆盖按钮时才向上移动视图

问题描述

当我点击 uitextview 时,我试图移动一个按钮。我使用了一个有效的代码。2tap 后我的按钮上升。

import UIKit
extension UIView{
    
    func bindKeyboard()  {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    }
    
    @objc func keyboardWillChange(_ notification: NSNotification) {
        // Allows for keyframe animation, moving the button up with the keyboard.
        // It binds the object with the button
        let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
        
        let startingFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let endingFrame =  (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let deltaY = endingFrame.origin.y - startingFrame.origin.y
        print(deltaY)
        
        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
            self.frame.origin.y += deltaY
        }, completion: nil)
    }
}

我有一个名为 nextBtn 的按钮。我正在调用 nextBtn.bindKeyboard() 方法。点击后,我得到了一个完美的按钮位置,但是当我尝试点击两次时,我得到了错误的位置。有时我的按钮不会向上移动。我不是以英语为母语的人。如果我犯了任何语法错误,请原谅我。 在第一次点击 textview 之后

第二次点击 textview 后

标签: iosswiftxcode

解决方案


如果尚未设置到该位置,则设置框架

extension UIView{
    
    func bindKeyboard()  {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
    }
    
    @objc func keyboardWillChange(_ notification: NSNotification) {
        // Allows for keyframe animation, moving the button up with the keyboard.
        // It binds the object with the button
        let duration = notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
        let curve = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as! UInt
        
        let startingFrame = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        let endingFrame =  (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let deltaY = endingFrame.origin.y - startingFrame.origin.y
        print(deltaY)
        
        
        if self.frame.origin.y == 0 {
        UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIView.KeyframeAnimationOptions(rawValue: curve), animations: {
            self.frame.origin.y += deltaY
        }, completion: nil)
    }
    }
}

在 didLoad 中添加绑定

override func viewDidLoad() {
        super.viewDidLoad()
         nextBtn.bindKeyboard()
        goalTextView.delegate=self
        

        // Do any additional setup after loading the view.
    }

从...除去textViewDidBeginEditing


推荐阅读