Search code examples
iphoneobjective-ciosios4uitextview

iOS - Scrolling two UITextViews simultaneously


I've done some looking around but I haven't been able to find anything that clearly explains how I could simultaneously scroll two un-editable UITextViews. I think I may need to use either scrollRangeToVisible, or setContentOffset, though I could not get either of them to work.

Does anyone have any examples/samples, or documentation regarding this that they could point me towards?

EDIT: To clarify, I would like to be able to scroll one UITextView, and have the changes as a result of the scrolling reflected on a second UITextView as well.

Thanks!


Solution

  • React in

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    

    and set the other's scrollView setContentVisible according to scrollView.contentOffset.

    Just be aware that some methods of UIScrollView will invoke scrollViewDidScroll even if called programmatically. This applies to scrollRangeToVisible and will end up in a loop unless you take action to prevent this loop. I don't think that setContentOffset or setting scrollView2.contentOffset = CGPointMake(..,..) does call scrollViewDidScroll. However, I would not sign this in blood. Be prepared to see a loop and take actions to avoid it. (such as boolean instance variable set before calling setContentOffset and re-set in scrollViewDidScroll followed by return;)