我有一些代码应该在键盘输入一些文本时提高容器视图。我觉得我肯定有 实现这个权利,但文本视图没有提高。容器视图不会升起
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冠军腰带上获得奖励。没有,但认真地我将不胜感激帮助