首页 > 点击button显示键盘

点击button显示键盘

需求如下:
点击UIButton弹出键盘,键盘的上方还需添加一个输入框(UITextField/UITextView)


http://blog.csdn.net/sy431256...
http://blog.csdn.net/oqqquzi1...
这两篇文章解决了我的问题
设置inputAccessoryView。


定义一个输入框类 在这个类内部定义它自身的show和dismiss方法

func show(inView: UIView) {          //inView是他将要显示的父视图
        inView.addSubview(self)
        snp_makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        setNeedsLayout()
        layoutIfNeeded()
        textView.becomeFirstResponder()
    }
    
    func dismiss() {
        textView.resignFirstResponder()
    }

同时可以订阅键盘的状态来改变他的位置

 /**
         *  键盘控制
         */
        keyboardHelper.keyboardWillChange
            .subscribeNext {[unowned self] (info) in
                var bottomOffset: CGFloat
                switch info.type {
                case .Show: bottomOffset = -(SCREEN_HEIGHT - info.frame.origin.y)
                case .Hide: bottomOffset = self.inputBar.bounds.size.height
                }
                self.cons_InputBarBottom?.updateOffset(bottomOffset)
                UIView.animateWithDuration(info.duration,
                    animations: {
                        if let cc = UIViewAnimationCurve(rawValue: info.animationCurve) {
                            UIView.setAnimationCurve(cc)
                        }
                        self.layoutIfNeeded()
                     }, completion: { (finished) in
                        switch info.type {
                        case .Hide: self.removeFromSuperview()
                        default:break
                        }
                })
        }.addDisposableTo(disposeBag)

一个UIView动画就可以了。

【热门文章】
【热门文章】