2017-08-12 2 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) 

はここで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

あなたのコードから見ることができるのは、キーボードが表示されている間、キーボードが表示されている間に個別の方法で行う必要がある単一の方法で上下スクロールを管理していることですここで

を解任され、各通知

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) 

キーボードが表示され、次の方法でイベントを消えるハンドルの異なるselectorを追加し、あなたに

を助けるかもしれないサンプルスニペットです

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

は、VARにLETからbottomConstraintを変更して、それを修正のように消えて行っている間、 はオブザーバーを削除するのを忘れないでください。忘れて、それを不変にしましょう。 WWEチャンピオンだと思うよ。

関連する問題