I added my UITextView inside UIStackView.
But my textview is not Multiline.
Auto layout is shown as below:
As per Image it is just taken 1 line. The textview frame shown as out of frame as per image.
UITextView
UIStackView
I added my UITextView inside UIStackView.
But my textview is not Multiline.
Auto layout is shown as below:
As per Image it is just taken 1 line. The textview frame shown as out of frame as per image.
UITextView
UIStackView
As long as your stack view is well-constrained, if you set isScrollEnabled
to false
on your UITextView
, it can size itself within the stack view.
The problem is that you have fixed the height
of UIStackview by giving top and bottom constraints.
Disable textview scrolling.
Make your textview delegate to self and implement textViewDidChange
Swift
func textViewDidChange(_ textView: UITextView) {
view.layoutIfNeeded()
}
Objective C:
-(void)textViewDidChange:(UITextView *)textView {
[self.view layoutIfNeeded];
}
Make sure this method get called.
Now your stackview should grow with your textview to multiline.
Then reason that your textview is not multiline is because you have fixed the height. So It will never be multiline.
See the GIF:
Best solution I have found for this so far is to wrap the UITextView in a UIView and then setting the fixed height of the UITextView with a height anchor.
let textView = UITextView()
let containerView = UIView()
textView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(textView)
textView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
textView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
textView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
textView.heightAnchor.constraint(equalToConstant: 100).isActive = true
let stackView = UIStackView(arrangedSubviews: [containerView])
© 2022 - 2024 β McMap. All rights reserved.