问题描述
基本上我正在尝试在inputAccessoryView属性中创建一个调整大小的UITextView。
Basically I am trying to create a resizing UITextView within a inputAccessoryView property.
我有一个viewController,方法canBecomeFirstResponder返回true,一个视图我通过自定义实例化class(从XIB获取)。在这个视图中是一个UITextView。
I have a viewController with the method canBecomeFirstResponder returning true and a view I instantiate via a custom class (which gets it from a XIB). Within this view is a UITextView located.
我尝试从该类内部调整完整的inputAccessoryView。我试过几个方面:直接设置框架,尝试使用高度约束。它似乎只调整了一半:
I try to resize the complete inputAccessoryView from inside of that class. I tried in in a few ways: setting the frame directy, try to use a height constraint. It appears it resizes only half way:
这基本上是我想要的(有或没有自动布局,但在iOS 7/8 +中工作):
class MessageInputAccessory: UIView, UITextViewDelegate
{
@IBOutlet weak var textView: UITextView!
func textViewDidChange(textView: UITextView)
{
var contentSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max))
self.frame.size.height = contentSize.height + 20
self.textView.reloadInputViews()
}
}
我发现这篇文章:。声明正在创建自动创建的约束。问题是,它不会为我创造约束。绝不禁用autolayout或启用autolayout(也可以通过setTranslatesAutoresizingMaskIntoConstraints())。
I have found this post: Changing the frame of an inputAccessoryView in iOS 8. Stating that a automatically created constraint is being created. The thing is, it does not create the constraint for me. In no way, with autolayout disabled or enabled (also via setTranslatesAutoresizingMaskIntoConstraints()).
推荐答案
它适用于iOS7 / iOS8 :
It works for me in iOS7/iOS8:
class MessageInputAccessory: UIView, UITextViewDelegate {
private var textView: UITextView!
private var heightConstraint: NSLayoutConstraint?
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
self.userInteractionEnabled = true
textView = UITextView()
textView.delegate = self
textView.bounces = false
textView.scrollEnabled = false
textView.layer.cornerRadius = 5
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.blueColor().CGColor
self.addSubview(textView)
}
override func layoutSubviews() {
super.layoutSubviews()
textView.frame = self.bounds
}
override func addConstraint(constraint: NSLayoutConstraint) {
self.heightConstraint = constraint
super.addConstraint(constraint)
}
func textViewDidChange(textView: UITextView) {
var contentSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max))
self.frame.size.height = contentSize.height
if let heightConstraint = self.heightConstraint {
heightConstraint.constant = self.frame.size.height
}
self.textView.reloadInputViews()
}
}
编辑:这也适用于 xib (iOS7 / iOS8):
This works with xib too (iOS7/iOS8):
class MessageInputAccessory: UIView, UITextViewDelegate {
@IBOutlet var textView: UITextView!
@IBOutlet var textViewHeightConstraint: NSLayoutConstraint!
private var heightConstraint: NSLayoutConstraint?
override func awakeFromNib() {
textView.layer.cornerRadius = 5
textView.layer.borderWidth = 1
textView.layer.borderColor = UIColor.blueColor().CGColor
}
override func layoutSubviews() {
textViewHeightConstraint.constant = self.bounds.size.height
super.layoutSubviews()
}
override func addConstraint(constraint: NSLayoutConstraint) {
if constraint.firstItem === self {
self.heightConstraint = constraint
}
super.addConstraint(constraint)
}
override func addConstraints(constraints: [AnyObject]) {
super.addConstraints(constraints)
}
func textViewDidChange(textView: UITextView) {
var contentSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max))
self.frame.size.height = contentSize.height
if let heightConstraint = self.heightConstraint {
heightConstraint.constant = self.frame.size.height
}
self.textView.reloadInputViews()
}
override func intrinsicContentSize() -> CGSize {
return self.bounds.size;
}
}
有我的xib:
There is my xib:
这不是一个很好的解决方案,但它的工作原理..请告诉我你是否知道更好的方法。
It is not a very good solution, but it works.. Please tell me if you know a better way.
这篇关于在iOS 8中动态调整inputAccessoryView的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!