unable to scroll the textview in Swift

7.9k views Asked by At

I have a TextView as shown below

enter image description here

I am not able to scroll the text view , I have added UITextViewDelegate in UIViewController class as well as set isUserInteractionEnabled property in textViewDidBeginEditing

func textViewDidBeginEditing(_ textView: UITextView) {
    textView.backgroundColor = UIColor.lightGray
    textView.isEditable = false
    textView.isUserInteractionEnabled = true
    textView.isScrollEnabled = true
}

What did I need to do?

Also, the scrolling is enabled in attribute inspector

enter image description here

4

There are 4 answers

0
Amarjit Dhillon On

This issue has occurred because the actual UITextView's size was more than screen size as shown below

enter image description here

0
AnthonyR On

The real answer is that the UITextView needs its content frame height inferior to its content size height to be able to scroll.

In your case, the content frame height is equal to the content size, so it doesn't scroll.

0
daj mi spokój On

It starts to get complicated when you have a UITextView on a UIView which is either a view on a UIScrollView or directly on a UIScrollview.

The best thing to do, is to give each of them their own actions when scrolling.

Firstly, make sure you have all of your delegates for EVERYTHING set. Make sure your UITextViews are not user restricted for what they don't need to be.

What I have to do in one of my published apps is this

func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
{
   if scrollView == textView1
   {
      // Do something if you actually want to, or just let textView1 scroll as intended
   }
   else if scrollView == textView2
   {
      // Do something if you actually want to, or just let textView2 scroll as intended
   }
   else if scrollView == zoomingScroll
   {
      // Do something if you need to or leave it
   }
   else
   {
      // Do something with all the other scrollable views if you need to
   }
}
0
ukw On

You just have to set the leading left, trailing, top space and bottom space to the View, but first make sure the Text View is smaller than the actual View (parent).