rebello95
rebello95

Reputation: 8576

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

Upvotes: 2

Views: 1819

Answers (2)

E. Rivera
E. Rivera

Reputation: 10938

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

Upvotes: 3

Viper
Viper

Reputation: 1408

Try this -

mytextView.contentSize = CGSizeMake(mytextView.frame.size.width,HEIGHT_YOU_WANT);

mytextView.showsHorizontalScrollIndicator = NO;

ALso take a look at SO Question may it help you.

Upvotes: 0

Related Questions