如何在 UITextView 中禁用垂直滚动? [英] How to disable vertical scrolling in UITextView?

查看:59
本文介绍了如何在 UITextView 中禁用垂直滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在 UITextView 中禁用垂直滚动同时保持水平滚动.

I just want to disable the vertical scrolling in my UITextView while keeping the horizontal scrolling.

我在这里发现了一些类似的问题,但是,似乎没有一个对我有用.我试过的是:

I found some similar questions here, however, none of them seemed to work for me. What I have tried is:

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

func makeTextView() {
    textView = UITextView()
    textView.frame = CGRect(x: 0, y: 50, width:self.view.frame.size.width, height:50)
    scrollView.addSubview(textView)
    NSLayoutConstraint.activate([
        textView.heightAnchor.constraint(equalToConstant: 50),
        textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        textView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    scrollViewDidScroll(textView)
}

即使在尝试之后,UITextView 仍然可以在垂直方向上滚动.

Even after trying this though, the UITextView still scrollable in the vertical direction.

推荐答案

您可以使用以下代码禁用 textview 的滚动:

You can use following code to disable scrolling of textview:

textView.isScrollEnabled = false

并在 UIScrollView 中添加你的 textView.它会给你你所期望的结果.

And add your textView inside UIScrollView. It will give you the result what you expected.

或者您可以通过以下方式以编程方式执行:

Or You can do programmatically as follows:

    var textView = UITextView()
    var scrollView = UIScrollView()
    func makeTextView() {
        scrollView.backgroundColor = .green
        textView.backgroundColor = .red

        textView.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."
        textView.isScrollEnabled = false
        scrollView.frame =  CGRect(x: 0, y: 0, width:self.view.frame.size.width, height:50)
        textView.frame = CGRect(x: 0, y: 0, width:self.view.frame.size.width, height:50)

        view.addSubview(scrollView)
        scrollView.addSubview(textView)

        scrollView.translatesAutoresizingMaskIntoConstraints = false
        textView.translatesAutoresizingMaskIntoConstraints = false

        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
        scrollView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor, constant: 0).isActive = true
        scrollView.heightAnchor.constraint(equalToConstant: 50).isActive = true

        textView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
        textView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0).isActive = true
        textView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
        textView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: 0).isActive = true
    }

这篇关于如何在 UITextView 中禁用垂直滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆