Search code examples
iosobjective-cios6uiscrollviewuitextview

Disable horizontal scrolling in UITextView


Hello: I'm building an app that supports iOS 6 and higher. I have a few UITextViews throughout the app, and I noticed that on iOS 6, the text views are able to be scrolled horizontally. On iOS 7, they can only be scrolled vertically. Is there a way to restrict scrolling so that it will only scroll vertically?

I've checked out some other similar questions, but I don't want to add a UILabel to a UIScrollView.

Any help is much appreciated!

EDIT

When using the following two lines (per the answers suggested), this still doesn't work when setting content insets. Anyone know how to fix this?

Attempt to disable scroll:

tView.contentSize = CGSizeMake(tView.frame.size.width, tView.contentSize.height);
tView.showsHorizontalScrollIndicator = FALSE;

Insets:

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
      tView.contentInset = UIEdgeInsetsMake(kTextViewInsets, kTextViewInsets, kTextViewInsets, kTextViewInsets);
} else {
      tView.textContainerInset = UIEdgeInsetsMake(kTextViewInsets, kTextViewInsets, kTextViewInsets, kTextViewInsets);
}

Solution

  • I would subclass UITextView and override setContentOffset:

    - (void)setContentOffset:(CGPoint)contentOffset
    {
        super.contentOffset = CGPointMake(0.0, // Ignore the passed offset. Could also use self.contentOffset.x
                                          contentOffset.y);
    }