Ken
Ken

Reputation: 1461

How to disable vertical scrolling in 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)
}

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

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

Upvotes: 4

Views: 6854

Answers (7)

Bhavik Modi
Bhavik Modi

Reputation: 1565

You can use following code to disable scrolling of textview:

textView.isScrollEnabled = false

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

Horizontal Scroll View


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
    }

Upvotes: 1

Matic Oblak
Matic Oblak

Reputation: 16774

An example of this can be made by placing your text view inside a scroll view. A working example of this would be the following:

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

}

Intentionally this is all done in code for readability but the same can be achieved with constraints.

Upvotes: 0

Dinesh Choudhary
Dinesh Choudhary

Reputation: 73

UITextView is a subclass of UIScrollView, So you can set textView contentSize like so:

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

With this, if you don't want horizontal scrolling you can set textView width and it's contentSize width equal and vise versa for vertical scrolling.

You can use it as per your need to restrict one of the scroll.

Upvotes: 0

fabcarvalhal
fabcarvalhal

Reputation: 36

Your code is correct, but you also need to set the textView delegate on makeTextView() like so:

textView.delegate = self

Also, don't forget to implement the UITextViewDelegate on your viewcontroller. Like this:

class MyViewController: UIViewController, UITextViewDelegate

Upvotes: 0

Victor G Moreno
Victor G Moreno

Reputation: 75

There are two main solutions:

  1. In your View controller, if you linked your textview to an outlet

    textView.isScrollEnabled = false
    
    
  2. In the storyboard (if you are not coding the UI)

    You can set the scrollEnabled option from true to false in the Attributes inspector on the right side of your screen if you clicked on the UITextView

Upvotes: 1

ZahraAsgharzade
ZahraAsgharzade

Reputation: 309

for vertical scrolling swift 4 :

    let origin: CGPoint = scrollView.contentOffset
    scrollView.contentOffset = CGPoint(x: origin.x, y: 0.0)
}

or you can change it like this :

textView.showsVerticalScrollIndicator = false

Upvotes: 0

Abhishek R
Abhishek R

Reputation: 269

UITextView is a subclass of UIScrollView, so you can prevent the view from scrolling by setting the property isScrollEnabled to false.

textView.isScrollEnabled = false

Upvotes: 2

Related Questions