2017-08-12 44 views
0

我有一些代碼應該在鍵盤輸入一些文本時提高容器視圖。我覺得我肯定有 實現這個權利,但文本視圖沒有提高。容器視圖不會升起

var bottomConstraint: NSLayoutConstraint? 


override func viewDidLoad() { 
    super.viewDidLoad() 
    navigationItem.title = "Comments" 
    collectionView?.backgroundColor = UIColor.white 
    self.navigationItem.hidesBackButton = true 
    let backButton = UIBarButtonItem(image: UIImage(named: "icons8-Back-64"), style: .plain, target: self, action: #selector(GoBack)) 
    self.navigationItem.leftBarButtonItem = backButton 

    self.collectionView?.register(CommentCell.self, forCellWithReuseIdentifier: cellID) 
    collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom:-50, right: 0) 

    collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -50, right: 0) 
    collectionView?.alwaysBounceVertical = true 
    collectionView?.keyboardDismissMode = .interactive 

    setupKeyboardObserver() 
    view.addSubview(containerView) 


    view.addConstraintsWithFormat("H:|[v0]|", views: containerView) 
    view.addConstraintsWithFormat("V:[v0(48)]", views: containerView) 

這裏IM的數設定爲0

let bottomConstraint = NSLayoutConstraint(item: containerView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0) 
    view.addConstraint(bottomConstraint) 


    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 

     NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

    // Register cell classes 
    // self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellID) 
    fetchComments() 

} 

這裏我要帶去的恆定控制,並使其反應的鍵盤高度

func handleKeyboardNotification(notification: NSNotification){ 
    if let userinfo = notification.userInfo{ 

     let keyboardFrame = (userinfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue 
     bottomConstraint?.constant = -(keyboardFrame?.height)! 

     let isKeyboardShowing = notification.name == NSNotification.Name.UIKeyboardWillShow 
     bottomConstraint?.constant = isKeyboardShowing ? CGFloat(-(keyboardFrame?.height)!) : CGFloat(0) 

    } 
} 

儘管這一切,鍵盤仍然覆蓋容器視圖。當我手動更改常數時,它會移動,但這些功能似乎對動態移動視圖沒有影響。我很困惑,任何幫助都會在WWE冠軍腰帶上獲得獎勵。沒有,但認真地我將不勝感激幫助

回答

0

我能夠從你的代碼,觀察什麼是您所管理的滾動從上到下,反之亦然單一的方法,你只需要做到這一點與各個方法之一,而鍵盤出現,另一種是在鍵盤被解僱

下面是示例代碼片段可以幫助你

添加不同selector每個通知

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

處理鍵盤出現,用下面的方法消失事件

func keyboardWillShow(notification: NSNotification) { 
     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
      if self.view.frame.origin.y == 0{ 
       UIView.animate(withDuration: 0.1, animations: {() -> Void in 
        self.view.frame.origin.y -= keyboardSize.height 
       }) 
      } 
     } 
    } 

    func keyboardWillHide(notification: NSNotification) { 
     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
      if self.view.frame.origin.y != 0{ 
       UIView.animate(withDuration: 0.1, animations: {() -> Void in 
        self.view.frame.origin.y += keyboardSize.height 
       }) 
      } 
     } 
    } 

注意: 不要忘了刪除觀察,而你的觀點是要像消失

NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
0

你handleKeyboardNotification函數是在函數的最後一行設置恆定回到0。

此外,你的常數不應該是負面的鍵盤值,它應該是鍵盤的高度加上一個保證金,如果你需要它。

0

通過讓改變bottomConstraint爲VAR固定它。忘記讓它成爲不變的。哈哈猜我是WWE冠軍

相關問題