如何禁用UITextView中的垂直滚动?

时间:2019-06-28 04:50:20

标签: ios swift scroll uitextview

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

我在这里找到了一些类似的问题,但是似乎没有一个对我有用。我尝试过的是:

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仍然可以在垂直方向滚动。

Here's an image of what my app and layout looks like, I hope it helps a little

7 个答案:

答案 0 :(得分:0)

UITextView是UIScrollView的子类,因此可以通过将属性isScrollEnabled设置为false来防止视图滚动。

textView.isScrollEnabled = false

答案 1 :(得分:0)

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

textView.isScrollEnabled = false

并在UIScrollView内添加textView。它将为您提供预期的结果。

Horizontal Scroll View


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

    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
    }

答案 2 :(得分:0)

用于垂直滚动library(tidyverse) n3 %>% arrange_at(1:4) %>% group_by_at(1:3) %>% mutate(recruits = sum(recruits)) %>% slice(n()) # A tibble: 12 x 10 # Groups: Module #, Side, TimeStep [12] # `Module #` Side TimeStep Date Year Site Treatment recruits Site_long Shelter # <fct> <fct> <int> <date> <fct> <fct> <chr> <dbl> <fct> <fct> # 1 114 N 4 2017-08-20 17 WAI CLO 6 Waikiki Low # 2 114 N 5 2017-11-12 17 WAI CLO 2 Waikiki Low # 3 114 N 6 2018-03-11 18 WAI CLO 1 Waikiki Low # 4 114 N 7 NA 18 <NA> <NA> 0 Waikiki Low # 5 114 S 4 2017-08-26 17 WAI CLO 5 Waikiki Low # 6 114 S 5 NA 17 <NA> <NA> 0 Waikiki Low # 7 114 S 6 2018-03-11 18 WAI CLO 2 Waikiki Low # 8 114 S 7 2018-05-31 18 WAI CLO 4 Waikiki Low # 9 114 T 4 2017-08-26 17 WAI CLO 2 Waikiki Low #10 114 T 5 NA 17 <NA> <NA> 0 Waikiki Low #11 114 T 6 2018-03-11 18 WAI CLO 1 Waikiki Low #12 114 T 7 NA 18 <NA> <NA> 0 Waikiki Low

destroyOnUnmount: false

或者您可以像这样更改它:

export default reduxForm({ form: 'form', destroyOnUnmount: false, })(Form)

答案 3 :(得分:0)

主要有两种解决方案:

  1. 在View控制器中,如果您将textview链接到插座

    textView.isScrollEnabled = false
    
  2. 在情节提要中(如果您未对UI进行编码)

    如果您单击UITextView

  3. ,则可以在屏幕右侧的“属性”检查器中将scrollEnabled选项从true设置为false。

答案 4 :(得分:0)

您的代码正确,但是您还需要像这样设置makeTextView()上的textView委托:

textView.delegate = self

此外,不要忘记在您的ViewController上实现UITextViewDelegate。像这样:

class MyViewController: UIViewController, UITextViewDelegate

答案 5 :(得分:0)

UITextView是UIScrollView的子类,因此您可以像这样设置textView contentSize

textView.contentSize = CGSize(width: , height: )

有了这个,如果您不想水平滚动,则可以设置textView的宽度,它的contentSize宽度相等,垂直滚动则相反。

您可以根据需要使用它来限制其中一个滚动。

答案 6 :(得分:0)

可以通过将文本视图放在滚动视图中来举例说明。一个可行的示例如下:

class ViewController: UIViewController {

    private var scrollView: UIScrollView!
    private var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        scrollView = {
            let scrollView = UIScrollView(frame: CGRect(x: 50.0, y: 150.0, width: 300.0, height: 100.0))
            scrollView.backgroundColor = UIColor.gray
            view.addSubview(scrollView)
            return scrollView
        }()

        textView = {
            let textView = UITextView(frame: .zero)
            scrollView?.addSubview(textView)
            textView.text = "Some fairly long text to be able to debug multiline and size being out of bounds of this item\nSome fairly long text to be able to debug multiline and size being out of bounds of this item\nSome fairly long text to be able to debug multiline and size being out of bounds of this item"
            textView.font = UIFont.systemFont(ofSize: 30)
            textView.backgroundColor = UIColor.green.withAlphaComponent(0.5)
            textView.sizeToFit()
            textView.isScrollEnabled = false
            return textView
        }()

        scrollView.contentSize = textView.bounds.size
        scrollView.delegate = self
    }

}

extension ViewController: UIScrollViewDelegate {

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

}

出于可读性的考虑,所有这些都是故意在代码中完成的,但可以通过约束来实现同样的目的。