UITextView - 禁用垂直滚动

发布于 2024-11-30 15:39:48 字数 44 浏览 2 评论 0原文

如何禁用 UITextView 中的垂直滚动?我希望它基本上只是水平滚动。

How can I disable vertical scrolling in my UITextView? I want it to basically just scroll horizontally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

╰沐子 2024-12-07 15:39:48

在某些情况下,当试图限制不需要的 UITextView 滚动时,我发现向 UITextView 委托添加类似以下内容很有帮助(这是一个 UIScrollView 委托方法,但当然,UITextView 继承自 UIScrollView)。这可能对你有用。

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

那么scrollEnabled属性呢?将scrollEnabled属性设置为NO会阻止用户滚动(双向),但有时系统会向UITextView发送setContentOffset:animated:消息。 scrollEnabled 属性适用于垂直和水平滚动。鉴于您的问题,您可能希望保持scrollEnabled不变。

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.

安穩 2024-12-07 15:39:48

您可以从 Xcode 更改它 -

点击此处

You can change it from Xcode -

Click here

羁〃客ぐ 2024-12-07 15:39:48

禁用 Swift 4 垂直滚动的解决方案:

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

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)
}
这个俗人 2024-12-07 15:39:48

如果您有自定义 textView 子类,则可以覆盖 -gestureRecognizerShouldBegin 以禁用滚动。

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

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];
        }
    }
}
歌入人心 2024-12-07 15:39:48

如果您不想垂直滚动,为什么不直接使用 UITextField 呢?

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

清晰传感 2024-12-07 15:39:48

只需将 contentSize 设置为视图的高度即可。

你将使用这个:

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

Just set the contentSize to the height of the view.

You'll use this:

CGSize scrollableSize = CGSizeMake(widthOfContent, heightOfView);
[myScrollView setContentSize:scrollableSize];
我乃一代侩神 2024-12-07 15:39:48

将 UITextView 放在 UIScrollView 中。
将 UITextView.frame 设置为完整文本适合一行的大小,并将 ScrollView 的 contenSize 设置为 UITextView.frame 的大小。

干杯
内茨

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文