ali*_*ego 13 ios autoresizingmask nslayoutconstraint inputaccessoryview swift
我已经花了好几天没有解决方案了.
我有inputAccessoryView
一个UIView
包含一个textView
和两个按钮.该行为符合inputAccessoryView
预期,除了一个以外在所有情况下都能正常工作.
当textView的高度增加时,我试图增加inputAccessoryView
相同数量的高度.当我重新定义的高度inputAccessoryView
中textViewDidChange
,该inputAccessoryView
增加的高度向下在键盘上,而不是向上.
我从SO那里尝试了许多不同的建议,但没有任何效果.我想这是自动添加NSLayoutConstraint
的,inputAccessoryView
但我不知道如何在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
}
Run Code Online (Sandbox Code Playgroud)
加入
inputAccessoryView.setTranslatesAutoresizingMaskIntoConstraints(true)
Run Code Online (Sandbox Code Playgroud)
到上面的代码帮助和inputAccessoryView高度正确向上增加但是我得到无法同时满足几个约束的约束,并且很难识别违规者.另外,我得到了textView在新行的每个第二个实例上创建额外空间的奇怪效果.
谢谢.
max*_*lov 34
要使输入附件视图垂直增长,您只需设置它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()
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5981 次 |
最近记录: |