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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
HumbleWebDev
  • 555
  • 4
  • 20

5 Answers5

101

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
Sune
  • 1,326
  • 1
  • 11
  • 17
  • 1
    This is the best answer for those using autolayout. – Dean Kelly May 10 '17 at 19:47
  • 1
    This should be the accepted answer. This was exactly my problem and this one liner fixed right away. – Mauricio Chirino Feb 21 '18 at 11:31
  • 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? – puja Mar 19 '18 at 11:22
  • @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. – Sune Mar 19 '18 at 14:20
  • @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). – Kheldar Dec 11 '18 at 11:05
4

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

MedMind
  • 51
  • 3
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.

saswanb
  • 1,975
  • 1
  • 17
  • 25
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.

bsmith11
  • 296
  • 1
  • 3
-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)
Lawrence413
  • 1,976
  • 1
  • 14
  • 28
  • 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 = true – HumbleWebDev Apr 28 '16 at 17:51
  • 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. – HumbleWebDev Apr 28 '16 at 18:00
  • 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. – Lawrence413 Apr 29 '16 at 07:14