gpt4 book ai didi

ios - 如何增加 inputAccessoryView 的高度

转载 作者:IT王子 更新时间:2023-10-29 05:22:31 25 4
gpt4 key购买 nike

我已经花了几天时间解决这个问题,看不到任何解决方案。

我有一个 inputAccessoryView,它由一个包含 textView 和两个按钮的 UIView 组成。 inputAccessoryView 的行为符合预期,并且在除一种情况外的所有情况下都可以正常工作。

当 textView 的高度增加时,我试图将 inputAccessoryView 的高度增加相同的量。当我在 textViewDidChange 中重新定义 inputAccessoryView 的高度时,inputAccessoryView 会在键盘上向下而不是向上增加高度。

我尝试了 SO 的许多不同建议,但没有任何效果。我猜这是 inputAccessoryView 自动添加的 NSLayoutConstraint 但我不知道如何在 swift 和 iOS 8.3 中更改该值。

func textViewDidChange(textView: UITextView) {

var contentSize = messageTextView.sizeThatFits(CGSizeMake(messageTextView.frame.size.width, CGFloat.max))

inputAccessoryView.frame.size.height = contentSize.height + 16

}

添加

inputAccessoryView.setTranslatesAutoresizingMaskIntoConstraints(true)

对上面的代码有帮助并且 inputAccessoryView 高度正确地向上增加但是我得到无法同时满足多个约束的约束并且很难识别违规者。此外,我还得到一个奇怪的效果,即 textView 在每两个新行的实例下方创建额外的空间。

谢谢。

最佳答案

要使输入附件 View 垂直增长,您只需设置它的 autoresizingMask = .flexibleHeight,计算它的 intrinsicContentSize,然后让框架完成剩下的工作。

代码:

class InputAccessoryView: UIView, UITextViewDelegate {

let textView = UITextView()

override init(frame: CGRect) {
super.init(frame: frame)

// This is required to make the view grow vertically
self.autoresizingMask = UIView.AutoresizingMask.flexibleHeight

// Setup textView as needed
self.addSubview(self.textView)
self.textView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView]))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[textView]|", options: [], metrics: nil, views: ["textView": self.textView]))

self.textView.delegate = self

// Disabling textView scrolling prevents some undesired effects,
// like incorrect contentOffset when adding new line,
// and makes the textView behave similar to Apple's Messages app
self.textView.isScrollEnabled = false
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override var intrinsicContentSize: CGSize {
// Calculate intrinsicContentSize that will fit all the text
let textSize = self.textView.sizeThatFits(CGSize(width: self.textView.bounds.width, height: CGFloat.greatestFiniteMagnitude))
return CGSize(width: self.bounds.width, height: textSize.height)
}

// MARK: UITextViewDelegate

func textViewDidChange(_ textView: UITextView) {
// Re-calculate intrinsicContentSize when text changes
self.invalidateIntrinsicContentSize()
}

}

关于ios - 如何增加 inputAccessoryView 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31822504/

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