美文网首页
键盘上方的bar

键盘上方的bar

作者: 古月思吉 | 来源:发表于2018-09-24 20:37 被阅读0次

    *如何让键盘上方的 EditBar 随着键盘移动?
    (1)添加监听、监听事件:

    //MARK: - 编辑bar相关
    extension PublishDynamicController {
        
        //添加键盘的监听
        func addKeyboardObserver() {
            //添加键盘监听
            NotificationCenter.default.addObserver(self, selector:#selector(self.keyboardWillChangeFrakeyboardWillChangeFrameNotificationmeNotification(note:)),name:NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
        }
    
        @objc func keyboardWillChangeFrakeyboardWillChangeFrameNotificationmeNotification(note : NSNotification) {
            // 1.获取动画执行的时间
            let duration = note.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
            
            // 2.获取键盘最终y值
            let endFrame = (note.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
            let y = endFrame.origin.y
            
            // 3.计算工具栏距离底部的间距
            let margin = UIScreen.main.bounds.height - y
            
            // 4.执行动画
            bottomHeightCons.constant = margin
            UIView.animate(withDuration: duration) {
                self.view.layoutIfNeeded()
            }
        }
    
    }
    

    (2)移除监听:

    deinit {
            NotificationCenter.default.removeObserver(self)
        }
    

    相关文章

      网友评论

          本文标题:键盘上方的bar

          本文链接:https://www.haomeiwen.com/subject/diyhoftx.html