Basically I am trying to create a resizing UITextView within a inputAccessoryView property.
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.
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:
This is basically what i want (with or without autolayout, but working in 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()
}
}
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()).
It works for me in iOS7/iOS8:
EDIT: This works with xib too (iOS7/iOS8):
There is my xib:
It is not a very good solution, but it works.. Please tell me if you know a better way.
A fairly simple fix for this is to not make the actual inputAccessoryView resize at all - if you know the maximum height you'll need for it, you can create it at that maximum height, make it transparent, and add a subview that resizes however you like.
The detail you need to make it work is that that the inputAccessoryView needs to be a UIView subclass containing the following:
(where self.innerView is your dynamically resizing subview).
This makes it claim taps inside the subview, but pass on any that fall outside it.
The main drawback to be aware of is that keyboard notifications will give you frames containing the entire max-height inputAccessoryView. So if you're using them to (eg) adjust insets, you need to do some extra math there.