1, 在viewdidload中注册键盘监听

        //注册键盘监听
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "openKeyboardListener:", name: UIKeyboardWillShowNotification, object: nil)

2,获取键盘的高度

    //键盘打开监听
    func openKeyboardListener(notifaction:NSNotification){
        //获取键盘高
        let  userInfo:NSDictionary=notifaction.userInfo!
        let  nsvalue:NSValue=userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey) as NSValue
        let  keyboardRect=nsvalue.CGRectValue()
        var  keyboardHeight=keyboardRect.origin.y
        
        var  pwdHeight=password.frame.origin.y
        
        var distence=keyboardHeight-pwdHeight
        
        if(distence<0){
                print(distence)
            //键盘遮挡了  密码框
            scrollView.setContentOffset(CGPointMake(0, scrollView.contentOffset.y+distence), animated: true)
        }
    }

 

posted on 2015-05-14 16:58  青年程序猿  阅读(441)  评论(0)    收藏  举报