UITextview inside UIStackView
Asked Answered
E

3

21

I added my UITextView inside UIStackView.

But my textview is not Multiline.

Auto layout is shown as below:

enter image description here

As per Image it is just taken 1 line. The textview frame shown as out of frame as per image.

UITextView

enter image description here enter image description here

UIStackView

enter image description here

Entablement answered 30/5, 2016 at 8:3 Comment(2)
So, what is the problem ? – Balsamiferous
@Lion, should It displays with multiline? – Entablement
Q
54

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.

Querida answered 18/9, 2019 at 22:40 Comment(5)
Cheers! This is a huge one. – Gawain
@Gawain happy to help! I guess that was pretty timely, too πŸ˜… – Querida
You helped keep my imposter syndrome at bay for another day. Thank you. – Gove
By well-constrained I assume you mean constrained without a fixed height or a top or bottom constraint? (Maybe only top OR bottom and not both?) – Aboveground
Thanks for this Clay, very helpful...however I have discovered that, for iOS 16 the app crashes if we set the isScrollEnabled property of the UITextView to false...a hack will be to set it to true in textViewDidBeginEditing method of the UITextView delegate and set it back to false in textViewDidEndEditing method – Charlinecharlock
P
30

The problem is that you have fixed the height of UIStackview by giving top and bottom constraints.

  1. delete the bottom constraint

    enter image description here

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:

enter image description here

Purebred answered 30/5, 2016 at 11:33 Comment(5)
Excellent! If you have static text, what I found is that if you override viewDidLayoutSubviews() that you can get the content height there and update the height constraint. But your answer got me on the right path! override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() textViewHeightConstraint.constant = textView.contentSize.height textView.layoutIfNeeded() } – Sukkoth
What if the stack view isn't the last view? For example, you can't delete the bottom constraint because you need to have a spacing to a lower positioned view. – Kelm
@TeodorCiuraru, If stackview isn't last view, then you have to give vertical spacing to the next view. You have to do it in such a way that the next view goes down or decrease height to create a room for this view(next view should not have the bottom constraint or fixed height). – Purebred
@Purebred Seems like I'm in bad luck. I was needing a bottom constraint on the last view. Luckily, I solved it by unembeding from the Stack View and randomly pushing compress hugging priority buttons :)) – Kelm
What if you want the textView to scroll? – Capitol
D
0

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])
Digenesis answered 26/10, 2017 at 18:6 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.