gpt4 book ai didi

ios - 容器 View 不会提升

转载 作者:行者123 更新时间:2023-11-30 12:14:00 24 4
gpt4 key购买 nike

我有一些代码应该在键盘输入一些文本时引发容器 View 。我感觉我肯定有实现了这个权利,但 TextView 没有提升。

 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()

}

在这里,我控制常量并使其对键盘高度使用react

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)

}
}

尽管如此,键盘仍然覆盖了容器 View 。当我手动更改常量时,它会移动,但这些函数似乎对动态移动 View 没有影响。我很困惑,任何帮助都会得到 WWE 冠军腰带的奖励。不,但说实话,我很感激您的帮助

最佳答案

我从您的代码中可以观察到,您正在使用单一方法管理从上到下的滚动,反之亦然,您只需要使用单独的方法来做到这一点,一个是在键盘出现时,另一个是在键盘出现时解雇

这里是示例片段,可能会对您有所帮助

为每个通知添加不同的选择器

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
})
}
}
}

注意:当你的 View 即将消失时,不要忘记删除观察者

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

关于ios - 容器 View 不会提升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45651758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com