Bilal
Bilal

Reputation: 588

Linking text views to scroll together

I have two side by side text views. How can I link them so that if one is scrolled down other automatically scrolls the same as first one in swift 2.

Upvotes: 2

Views: 652

Answers (1)

Leo
Leo

Reputation: 24714

Set your viewController conforms to UITextViewDelegate,then set textView delegate to self,then in scrollViewDidScroll sync both contentOffSet

For example Gif

enter image description here

Code

class ViewController: UIViewController,UITextViewDelegate {

@IBOutlet weak var textview2: UITextView!
@IBOutlet weak var textview1: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()
    textview1.delegate  = self
    textview2.delegate  = self
    // Do any additional setup after loading the view, typically from a nib.
}
func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView == textview1{
        textview2.contentOffset = textview1.contentOffset
    }else{
        textview1.contentOffset = textview2.contentOffset
    }
}

}

Upvotes: 7

Related Questions