关闭时如何将 UIToolBar 修复到键盘顶部

Posted

技术标签:

【中文标题】关闭时如何将 UIToolBar 修复到键盘顶部【英文标题】:How to fix UIToolBar to top of keyboard when dismissing 【发布时间】:2021-07-07 08:59:56 【问题描述】:

我已经为键盘实现了滑动关闭,我希望 UITextField/UIToolBar 在键盘向下滑动的同时向下移动,所以基本上将 UIToolbar 固定在键盘的顶部。目前 UIToolbar 保持在它的位置,直到键盘完全关闭..

【问题讨论】:

【参考方案1】:

从一些代码开始会很不错...但这是一个新项目,所有代码都应该按照您的要求完成。

class ViewController: UIViewController 
    
    private var bottomConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() 
        super.viewDidLoad()
        
        let bottomView = UIToolbar(frame: .init(x: 0.0, y: view.bounds.height-50.0, width: view.bounds.width, height: 50.0))
        bottomView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(bottomView)
        view.addConstraint(.init(item: bottomView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 0.0))
        view.addConstraint(.init(item: bottomView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: 0.0))
        let bottomConstraint: NSLayoutConstraint = .init(item: bottomView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0.0)
        view.addConstraint(bottomConstraint)
        view.addConstraint(.init(item: bottomView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0))
        self.bottomConstraint = bottomConstraint
        
        let textField = UITextField(frame: .init(x: 0.0, y: 0.0, width: view.bounds.width, height: 50.0))
        textField.backgroundColor = .lightGray
        bottomView.items = [UIBarButtonItem(customView: textField)]
        
        view.addGestureRecognizer(UITapGestureRecognizer(target: textField, action: #selector(resignFirstResponder)))
        
        NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardChange), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
    
    
    @objc private func onKeyboardChange(notification: NSNotification) 
        guard let info = notification.userInfo else  return 
        guard let value: NSValue = info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else  return 
        
        let newFrame = value.cgRectValue
        
        let panel: UIView = self.view // Use whatever is the superview of constrained view
        let height = max(0, panel.bounds.height - panel.convert(newFrame.origin, from: nil).y)
        
        if let durationNumber = info[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
            let keyboardCurveNumber = info[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber 
            let duration = durationNumber.doubleValue
            let keyboardCurve = keyboardCurveNumber.intValue
            let curve: UIView.AnimationCurve = UIView.AnimationCurve(rawValue: keyboardCurve) ?? .linear
            let options = UIView.AnimationOptions(rawValue: UInt(curve.rawValue << 16))
            
            UIView.animate(withDuration: duration, delay: 0, options: options, animations: 
                self.bottomConstraint?.constant = -height
                panel.layoutIfNeeded()
            , completion: nil)

         else 
            self.bottomConstraint?.constant = -height
            panel.layoutIfNeeded()
        
    


我会在故事板中做事并使用插座...但这应该让您更好地了解事情的设置方式。

【讨论】:

以上是关于关闭时如何将 UIToolBar 修复到键盘顶部的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UIPickerView 上添加 UIToolbar? (iPhone/iPad)

键盘弹出时网页滚动[关闭]

iOS UIToolBar 隐藏在键盘上

如何在IOS键盘顶部的键盘上添加完成按钮?

使用 UIToolbar 帮助修复 UIViewController

在 Swift 中关闭键盘时,UIToolbar 不再显示在视图中