2017-08-12 43 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冠军

相关问题