Xcode - Swift, UITextView won't display on UIStackView
Asked Answered
L

5

20

I know that there's a question similar to this already, but his solution didn't solve my problem.

What I have is, I've used the storyboard to create a view controller, and then place a child stack view ontop.

Using IBOutlet, I've linked the UIStackView to the custom view controller class.

Everything's linked together correctly, this isn't the issue (I've made sure).

My problem is I can't seem to get a UITextView to display in the UIStackView.

here's my code (inside a function in the view controller):

let textView = UITextView()
textView.text = "test"
stackView.addArrangedSubview(textView) //stackView is the IBOutlet

I've been able to make the UITextView appear on the parent view using

let pos = CGRect(x: 100, y: 100, width: 200, height: 200)
let textView = UITextField(frame: pos)
textView.text = "test"
//stackView.addArrangedSubview(textView)
self.view.addSubview(textView)

And no, it doesn't work to uncomment the line with stackView, then comment out the self.view.addSubview

But I manage to get something to appear in the UITabView if I use a UITextField. This is beyond annoying....

Any suggestions?

Lesko answered 27/4, 2016 at 18:0 Comment(0)
F
104

The stack view does not know the size of the text view because it is scrollable by default. You need to disable scrolling:

textView.isScrollEnabled = false
Fallacious answered 8/11, 2016 at 12:49 Comment(5)
This is the best answer for those using autolayout.Skippie
This should be the accepted answer. This was exactly my problem and this one liner fixed right away.Mucoid
Hey still i have little issue i.e textview height is managed based on text assigned to it but i want to display that height based on stackview's bottomanchor constraint. So how it can be managed?Busywork
@puja: I'm not sure I understand your problem, but it sounds like you might want to take a look at setting the distribution property of the stackview.Fallacious
@MauricioChirino not if you need the scrolling behavior, in which case Medmind's answer is a better option (eg, setting the positioning constraint's explicitly).Malinowski
E
5

You have to add contraints of 0 left and right from text view to the stack view.

Eigenfunction answered 28/6, 2018 at 20:44 Comment(0)
S
2

Disabling scrolling did not work for me, but setting a defined height using a height constraint worked for me. I did not use subviews - a height constraint directly on the UITextView is all that was needed in my case.

Sniffy answered 8/2, 2018 at 16:48 Comment(0)
B
1

You should not be mixing manual frame setting with AutoLayout (what UIStackView uses internally). @Lawrence413's answer is wrong and is using UIStackView incorrectly.

Below is a blog post describing UIStackView and how to use it: http://www.raizlabs.com/dev/2016/04/uistackview/

Take note of the UIStackViewDistribution and how it is used to resize arrangedSubviews.

Bingham answered 29/4, 2016 at 19:27 Comment(0)
O
-2

You need a frame for the UITextView. And use addSubview instead of addArrangedSubview.

I used the following and a text view appeared just fine in the stack view:

let textView = UITextView(frame: CGRectMake(0, 0, 100, 100))
textView.text = "test"
textView.backgroundColor = UIColor.redColor()
stackView.addSubview(textView)
Ojibwa answered 27/4, 2016 at 18:42 Comment(3)
Thank you very much. This worked for me. It's weird though, an online tutorial I read said avoid addSubview with stackview and use addArrangedSubview. This seems to be the correct solution. Edit: There seems to be a new issue now though, the UITextView won't edit. And yes I add the line textView.editable = trueLesko
Edit2: This also seems to break the functionality of UIStackView entirely, as it's supposed to auto stack new elements when added. If you use this method to add a second UITextView, they layer ontop one another.Lesko
They layer on top of each other because they have the same coordinates (x:0, y:0) (in case you set the same frame). You can try just setting the width and height of the text view and see if the stack view automatically arranges it. I tried addArangedSubview but the text view didn't appear so that's why I suggested that. If you would tell us specifically what you want to achieve, people could help you more precisely. Do you want to add programmatically more text views in a stack view? I'll look into my code and see how I can help you.Ojibwa

© 2022 - 2024 — McMap. All rights reserved.