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
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.
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.
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.
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.
delete the bottom constraint
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With