开发者

UITextView - disable vertical scrolling

How can I disable vertical scrolling in my UITextView? I want it to basically just scroll horiz开发者_StackOverflowontally.


In some circumstances, when trying to clamp down on unwanted UITextView scrolling I have found it helpful to add something like the following to the UITextView delegate (this is a UIScrollView delegate method but, of course, UITextView inherits from UIScrollView). This might work for you.

- (void)scrollViewDidScroll:(id)scrollView
{
      CGPoint origin = [scrollView contentOffset]; 
      [scrollView setContentOffset:CGPointMake(origin.x, 0.0)];
}

What about the scrollEnabled property? Setting the scrollEnabled property to NO stops the user from scrolling (in both directions), but there are occasions where the system sends setContentOffset:animated: messages to a UITextView. The scrollEnabled property applies to both vertical and horizontal scrolling. Given your question, you might want to leave scrollEnabled as is.


You can change it from Xcode -

UITextView - disable vertical scrolling


Solution for disabling vertical scrolling for Swift 4:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let origin: CGPoint = scrollView.contentOffset
    scrollView.contentOffset = CGPoint(x: origin.x, y: 0.0)
}


If you have your custom textView subclass, you can override -gestureRecognizerShouldBegin to disable the scroll.

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        if (gestureRecognizer.view == self)
        {
            return NO;
        }
        else
        {
            return [super gestureRecognizerShouldBegin: gestureRecognizer];
        }
    }
}


why not just use a UITextField if you dont want vertical scrolling?


Just set the contentSize to the height of the view.

You'll use this:

CGSize scrollableSize = CGSizeMake(widthOfContent, heightOfView);
[myScrollView setContentSize:scrollableSize];


place your UITextView in a UIScrollView. Set your UITextView.frame to a Size the complete Text fits in a Line and set the contenSize of the ScrollView to the size of your UITextView.frame.

Cheers nettz

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜