Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextview inside UIStackView

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

like image 932
user2526811 Avatar asked May 30 '16 08:05

user2526811


People also ask

How to add stackview in storyboard?

To use a stack view, open the Storyboard you wish to edit. Drag either a Horizontal Stack View or a Vertical Stack View out from the Object library, and position the stack view where desired. Next, drag out the stack's content, dropping the view or control into the stack.

Why use UIStackView?

UIStackView is useful when you need to repeat same views multiple times like in Sing up view. We use many textfields and manually set constraints between each textfields. But if you put all textfields in stack view then you just need to set required constraints of stackview only and not textfields.

How to use stackview?

Xcode provides two ways to use stack view: You can drag a Stack View (horizontal / vertical) from the Object library, and put it right into the storyboard. You then drag and drop view objects such as labels, buttons, image views into the stack view. Alternatively, you can use the Stack option in the auto layout bar.


2 Answers

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.

like image 164
Clay Ellis Avatar answered Sep 17 '22 21:09

Clay Ellis


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

like image 37
Irfan Avatar answered Sep 16 '22 21:09

Irfan