找到你要的答案

Q:Enabling scroll on UITextView

Q:在UITextView使滚动

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!
     }
  }

我创造了一个containerview和UITextView作为子视图的一个UIScrollView UIScrollView。我scrollenabled TextView和初始化设置为true。现在基于contentoffset。Y ScrollView,我想保持scrollenabled切换的方法。但由于某些原因,一旦我scrollenabled虚假,我似乎不能使滚动一次…

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!
     }
  }
answer1: 回答1:

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.

我面临着同样的问题,并最终解决它通过继承UITextView如下所示:

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
        }
    }
}

在init(或界面生成器),设置你的实例允许滚动的方法。为了不滚动,设置“displayscrolling VaR为false。当你想滚动,集“displayscrolling '真。

answer2: 回答2:

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

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

ios  swift  uiscrollview  uitextview