美文网首页
弹出键盘时,底部按钮一起动

弹出键盘时,底部按钮一起动

作者: 不是谁的李逍遥 | 来源:发表于2017-02-07 17:02 被阅读34次

    //viewWillAppear

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(true)
            NotificationCenter.default.addObserver(self, selector: #selector(keyBoardChange(notification:)), name:NSNotification.Name.UIKeyboardDidChangeFrame, object: nil)
            //  NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyBoardDidHide(_:)), name:UIKeyboardDidHideNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
        }
    
    //MARK:键盘悬浮处理
        func keyBoardChange(notification :NSNotification){
            
        }
        
        
        func keyBoardWillShow(notification :NSNotification){
            
            // print("键盘将要显示")
            let dict:NSDictionary = notification.userInfo! as NSDictionary
            let aValue = dict.object(forKey: UIKeyboardFrameEndUserInfoKey)
            let keyboardRect = (aValue! as AnyObject).cgRectValue
            let keyHeight =  keyboardRect?.size.height
            self.saveBtn.frame = CGRect(x: 0, y: SCREEN_HEIGHT-48-keyHeight!, width: SCREEN_WIDTH, height: 48)
        }
        
        func keyBoardWillHide(notification :NSNotification){
            saveBtn.frame = CGRect(x: 0, y: XY_SCREEN_HEIGHT - 48 , width: XY_SCREEN_WIDTH, height: 48)
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            NotificationCenter.default.removeObserver(self)
            
        }```

    相关文章

      网友评论

          本文标题:弹出键盘时,底部按钮一起动

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