1
votes

I have created a UIScrollView in a ContainerView and a UITextView as a subview of UIScrollView. I initialize the textView with scrollEnabled set to true. Now based on the contentOffset.y of the scrollView, I want to keep toggling scrollEnabled on the textView. But for some reason, once I set scrollEnabled to false, I can't seem to enable scroll again...

override func viewDidload() {
    self.scrollView  = UIScrollView(frame: CGRectMake(...))
    self.scrollView.delegate = self
    self.view.addSubview(self.scrollView)
    self.textView    = UITextView(frame: CGRectMake(...))
    self.textView.scrollEnabled = true
    self.textView.userInteractionEnabled = true
    self.scrollView.addSubview(self.textView)

}

func scrollViewDidScroll(scrollView: UIScrollView) {
   if self.scrollView.contentOffset.y >= 180 {
        self.textView.scrollEnabled  = true // This does not work!
     } else {
     self.textView.scrollEnabled  = false // This works!
     }
  }
2
If you disable the scrolling, the contentOffset cannot grow anymore, so the first condition can't be satisfied anymore. Why do you want to disable/enable the scrolling? Maybe there is another solution to your problem than this.Catalina T.
I am disabling scrolling on textView not scrollView. So contentOffset is not a problem...I have a menu(UIButtons) around half way down the screen. When user starts scrolling, user should be able to scroll menu to top and only then textView starts scrolling.NS Spartan029
Could you add some logging in the scrollViewDidScroll method to see what the contentOffset is, if it ever gets to be 180? Or just add a breakpoint in the if, to see if the condition is ever satisfied. Maybe the contentSize of the scrollView is not bigger than 180?Catalina T.
It does get satisfied... But once i set textView.scrollEnabled to false, it just doesn't want to scroll againNS Spartan029

2 Answers

1
votes

I was faced with the same problem, and eventually dealt with it by subclassing UITextView as shown below:

class TextView: UITextView {

    var displayScrolling: Bool = true {
        didSet {
            self.showsVerticalScrollIndicator = displayScrolling
        }
    }

    override var contentOffset: CGPoint {
        set {
            if displayScrolling {
                super.contentOffset = newValue
            }
        }
        get {
            return super.contentOffset
        }
    }
}

At init (or in interface builder), set your instance of TextView to allow scrolling. In order to not scroll, set the 'displayScrolling' var to false. When you want to scroll, set 'displayScrolling' to true.

-3
votes

It will be scroll enabled if the text is longer.
I wish you luck!