Do I have to build in support for user scrolling through a UITextView object?

I am trying to add a UITextView within my app to output data to. Naturally the data will eventually be bigger than the size of the UITextView, and the view is a set size. So I would like the user to be able to scroll through its content.

However, I cannot scroll through the content in the app. Am I supposed to build the scrolling function myself? Seems weird that I would have to do that, but I cannot seem to find the answer to this on the web.

I’ve also noticed that no vertical scroll at shows up when the text count is larger than the size of the object, which makes me wonder if I am missing a property or two.

func createStatusField() -> UITextView {
    let myStatus = UITextView(frame: CGRect(x: 50, y: 50, width: 100, height: 300))

    myStatus.autocorrectionType = .no
    myStatus.text = "hello there"
    myStatus.backgroundColor = .secondarySystemBackground
    myStatus.textColor = .secondaryLabel
    myStatus.font = UIFont.preferredFont(forTextStyle: .body)
    myStatus.layer.zPosition = 1
    myStatus.isScrollEnabled = true
    myStatus.showsVerticalScrollIndicator = true

    return myStatus
}

Accepted Reply

So I went through everything one by one, i.e. constraints, buttons, etc.

When I removed the UIView, within the UIViewController, then the scrolling worked. I'm guessing that somehow the UIView was steal or preventing the gesture recognizers from working? Buttons got their clicks, but that's a different creature.

Replies

UITextView is a subclass of UIScrollView so, no, you don't need to build in the scrolling behavior yourself.

Likely, the only way of finding out what's wrong is via a sample project. However, apart from that, there are a couple of things you could look at.

It's sometimes helpful to use Xcode's view debugger to visualize the size and placement of your views. (For this purpose, I recommend turning off the clipping of the visualization to the bounds of the visible screen.)

Also, the problem may not be in the UITextView, but in the view that it's a subview of. For example, if a scroll view has grown large enough to need no scrolling, but is embedded in a smaller view, it'll look like scrolling is broken, though that's not the real reason.

Anyway, further debugging is called for. Keep in mind that UIScrollView (and therefore UITextView) has contentSize and contentOffset properties that you can examine.

Here's my problem: I built a sample project to paste here. In it, everything works (scrolling wise). In my large bloated project it doesn't. I copied and pasted the UITextView code, so I know it's the same.

So now I am really thrown. Could this have anything to do with the UIViewController? My large project has a UIView within the UIViewController, the sample does not.

I constrain the UIView to the UIViewController's safeAreaLayoutGuide, then I place all my buttons within the UIView. But everything is a subview of the UIVewController, so not sure how that might be affecting this.

Any clues where to look would be so appreciated.

So I went through everything one by one, i.e. constraints, buttons, etc.

When I removed the UIView, within the UIViewController, then the scrolling worked. I'm guessing that somehow the UIView was steal or preventing the gesture recognizers from working? Buttons got their clicks, but that's a different creature.