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

2017-02-07  本文已影响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)
        
    }```
上一篇下一篇

猜你喜欢

热点阅读