gpt4 book ai didi

ios - 如何在应用程序切换器/关闭器栏上方设置输入附件 View 的约束?

转载 作者:行者123 更新时间:2023-11-28 13:24:09 27 4
gpt4 key购买 nike

我很难将我的 inputAccessoryView 限制在 iPhoneX 屏幕(和其他缺口设备)的底部安全区域上方。我有一个 UICollectionViewController,它的 inputAccessoryView 是一个 UIView,其中包含一个 UITextView 和 2 个 UIButtons。基本上,该布局适用于聊天应用程序。当我运行当前代码时,inputAccessoryView 没有出现在屏幕底部的安全区域上方。它目前固定在屏幕底部。附截图。 enter image description here

如您所见,底部的应用程序切换器/关闭器栏与我的 inputAccessoryView 重叠。我想让 inputAccessoryView 位于上述栏之上。

这是我当前的代码(与约束相关):

class NewMessagesViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
private lazy var inputTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Enter message"
textField.translatesAutoresizingMaskIntoConstraints = false
textField.delegate = self
return textField
}()
private lazy var inputContainerView: UIView = {
let margin = view.layoutMarginsGuide
let safeArea = view.safeAreaInsets

let containerView = UIView()
containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
containerView.backgroundColor = .white

let uploadImageView = UIImageView()
uploadImageView.isUserInteractionEnabled = true
uploadImageView.image = UIImage(named: "image")
uploadImageView.translatesAutoresizingMaskIntoConstraints = false
uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleUploadTap)))
containerView.addSubview(uploadImageView)
uploadImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
uploadImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
uploadImageView.widthAnchor.constraint(equalToConstant: 44).isActive = true
uploadImageView.heightAnchor.constraint(equalToConstant: 44).isActive = true

let sendButton = UIButton(type: .system)
sendButton.setTitle("Send", for: .normal)
sendButton.translatesAutoresizingMaskIntoConstraints = false
sendButton.addTarget(self, action: #selector(sendText), for: .touchUpInside)
containerView.addSubview(sendButton)
// x,y,w,h
sendButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
sendButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
sendButton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

containerView.addSubview(self.inputTextField)
self.inputTextField.leftAnchor.constraint(equalTo: uploadImageView.rightAnchor, constant: 8).isActive = true
self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
self.inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
self.inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

let separatorLineView = UIView()
separatorLineView.backgroundColor = UIColor(red: 220, green: 220, blue: 220)
separatorLineView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(separatorLineView)
separatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
separatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
separatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
separatorLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true

return containerView
}()

override var inputAccessoryView: UIView? {
get {
return inputContainerView
}
}

override var canBecomeFirstResponder: Bool {
return true
}

override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.title = ticket.subject

collectionView.keyboardDismissMode = .interactive
}
}

我已经尝试将 UITextField 和 UIButton 的 底部 anchor 约束约束为等于 viewDidLoad() 中的 view.layoutMarginsGuide.bottomAnchor 但这只是使我的应用程序因错误而崩溃:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x28204dac0 "UIImageView:0x107417780.bottom"> and <NSLayoutYAxisAnchor:0x2820270c0 "UILayoutGuide:0x280c8cb60'UIViewLayoutMarginsGuide'.bottom"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'

如果有人可以提供有关如何正确限制应用程序切换器/关闭器栏上方的 inputAccessoryView 的代码片段,我们将不胜感激。

最佳答案

使用 SafeArea 设置 CollectionView 底部约束,这将解决您的问题。

关于ios - 如何在应用程序切换器/关闭器栏上方设置输入附件 View 的约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58671730/

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