0

I have a view controller with 8 to 9 textEdits where user has to fill them out to save to a database but it is taking a lot of my screen and some TE is not being shown because of the size of the iphones screen.I then decide to add a UIScrollView like this :

lazy var myScrollView : UIScrollView = {
    let scrol = UIScrollView()

    scrol.contentSize.height = 10000
    scrol.backgroundColor = appBackgroundColor
    scrol.translatesAutoresizingMaskIntoConstraints = false

    return scrol
}()

...

view.addSubview(myScrollView)
myScrollView.addSubview(labelYear)
myScrollView.addSubview(labelAppTitle)

// then I added the constraints 
NSLayoutConstraint.activate([
    myScrollView.topAnchor.constraint(equalTo: view.topAnchor),
    myScrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
    myScrollView.rightAnchor.constraint(equalTo: view.rightAnchor),
    //enter code here
    myScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    labelAppTitle.leftAnchor.constraint(equalTo: myScrollView.leftAnchor,constant: 40),
    labelAppTitle.topAnchor.constraint(equalTo: myScrollView.safeAreaLayoutGuide.topAnchor, constant: 10),
    labelAppTitle.rightAnchor.constraint(equalTo:myScrollView.rightAnchor, constant: -40),
    labelAppTitle.heightAnchor.constraint(equalToConstant: 90)
])

I have a lot more textEdits but I am not posting for sake of saving space.The problem is that it is not scrolling down like I wanted . How do I do this?

thank you

EdoBen
  • 1,676
  • 2
  • 14
  • 24
Eduardo
  • 3
  • 3
  • See this previous answer. [Programmatic UIScrollview with Autolayout](https://stackoverflow.com/questions/48216808/programmatic-uiscrollview-with-autolayout). It will help explain how to add your `UITextField`s to the scrollView and ensure that it scrolls. – Dennis W. Apr 11 '19 at 21:31
  • Also since you have so many text fields you might want to consider using `UITableViewController` instead with a cell for each text field, it gives you keyboard handling for free. – Dennis W. Apr 11 '19 at 21:34

1 Answers1

0
import UIKit

class TestController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        initUI()
    }

    func initUI() {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.isUserInteractionEnabled = true
        view.addSubview(scrollView)

        let contentView = UIView()
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.isUserInteractionEnabled = true
        contentView.isMultipleTouchEnabled = true
        scrollView.addSubview(contentView)

        let titleText = UITextField(frame: CGRect.zero)
        titleText.translatesAutoresizingMaskIntoConstraints = false
        titleText.borderStyle = .roundedRect
        titleText.isEnabled = true
        titleText.isUserInteractionEnabled = true
        titleText.placeholder = "Constants.Messages.titlePlaceholder"
        titleText.isUserInteractionEnabled = true
        titleText.delegate = self
        contentView.addSubview(titleText)

        // scroll view
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8.0),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8.0),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0)
            ])
        // content view
        NSLayoutConstraint.activate([
            contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
            ])
        // title text field
        NSLayoutConstraint.activate([
            titleText.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20.0),
            titleText.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
            titleText.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8.0),
            titleText.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0)
            ])
    }
}

This is an exmaple of using scrollView. When you create a scrollView, apple recommends to put a contentView in it and put it inside in scrollView and don't forget to use bottomAnchor. If you forgot to use that then it'll not scroll.

Habin Lama
  • 529
  • 5
  • 19