Resize Inputaccessoryview Dynamically in iOS 8

Resize inputAccessoryView dynamically in iOS 8

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()
}
}

EDIT: 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;
}
}

There is my xib:
Sample Image

It is not a very good solution, but it works.. Please tell me if you know a better way.

Change height of inputAccessoryView issue

Finally, i found the answer. In ios8, apple add a NSContentSizeLayoutConstraints to inputAccessoryView and set a constant with 44. You can't remove this constaint, because ios8 use it to calculate the height of inputAccessoryView. So, the only solution is to change value of this constant.

Example

in ViewDidAppear

- (void)viewDidAppear:(BOOL)animated {
if ([self.inputAccessoryView constraints].count > 0) {
NSLayoutConstraint *constraint = [[self.inputAccessoryView constraints] objectAtIndex:0];
constraint.constant = CommentInputViewBeginHeight;
}
}

change inputAccessoryView height when the textview height changed

- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height {

NSLayoutConstraint *constraint = [[self constraints] objectAtIndex:0];
float new_height = height + _textView_vertical_gap*2;

[UIView animateWithDuration:0.2 animations:^{
constraint.constant = new_height;
} completion:^(BOOL finished) {
[self setHeight:new_height];
[self reloadInputViews];
}];
}

That is.

how can i increase the height of an inputAccessoryView

To make input accessory view grow vertically you just set its autoresizingMask = .flexibleHeight, calculate its intrinsicContentSize and let the framework do the rest.

The code:

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()
}

}

ios8 swift: how can i access/edit/remove default/private inputAccessoryView height constraint '_UIKBAutolayoutHeightConstraint'

I solved it. I was searching for it in ViewDidLoad before the default constraint was setup.

iOS 11 inputAccessoryView broken

Solved it. It looks like UIToolbar's just are not working correctly in iOS 11.

Changed it to an UIView and removed

self.inputFieldView.isTranslucent = false
self.inputFieldView.setShadowImage(UIImage(), forToolbarPosition: .any)
self.inputFieldView.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)

got it working (and changed it to a UIView from UIToolbar in the xib as well.)



Related Topics



Leave a reply



Submit