swiftuiscrollviewuitextfielduilabeluiscrollviewdelegate

textField will not move in programable scrollview


I have a UIScrollView. I have a UITextField and a UILabel. For some reason my UITextField it will not move in my scroll view but my UILablel will. I have added the UITextField the same way as I added my UILabel to the scrollview using scrollView.addSubview(textField). Anyone see what I am doing wrong on this one?

import Foundation
import UIKit

//here
protocol AddContactDelegate {
func addContact(contact: Contact)
}



class AddContactController: UIViewController {

//delegate
var delegate: AddContactDelegate?

//TextField
let textField: UITextField = {
    let tf = UITextField()
    tf.placeholder = "Add Your Workout Title"
    tf.textAlignment = .center
    tf.translatesAutoresizingMaskIntoConstraints = false
    return tf
}()


override func viewDidLoad() {
    super.viewDidLoad()

    //making scroll view
    let screensize: CGRect = UIScreen.main.bounds
    let screenWidth = screensize.width
    let screenHeight = screensize.height
    var scrollView: UIScrollView!
    scrollView = UIScrollView(frame: CGRect(x: 0, y: 120, width: screenWidth, height: screenHeight))

    scrollView.contentSize = CGSize(width: screenWidth, height: 2000)
    view.addSubview(scrollView)


    //setting up how view looks-----
    view.backgroundColor = .white

    //top buttons
   self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(handleDone))

   self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(handleCancel))

    //view elements
    view.addSubview(textField)
    scrollView.addSubview(textField)
    textField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    textField.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    textField.widthAnchor.constraint(equalToConstant: view.frame.width - 64).isActive = true
   textField.becomeFirstResponder()

    //excercise label
    let excerciseNumber = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
    excerciseNumber.center = CGPoint(x: view.frame.width / 2 , y: view.frame.height / 20)
    excerciseNumber.textAlignment = .center
    excerciseNumber.text = "Workout Title"
    self.view.addSubview(excerciseNumber)
    scrollView.addSubview(excerciseNumber)

}

   //done button
@objc func handleDone(){
    print("done")

    guard let fullname = textField.text, textField.hasText else {
        print("handle error here")
        return
    }

    let contact = Contact(fullname: fullname, hello: "hi")
    delegate?.addContact(contact: contact)
    print(contact.fullname)
    print(contact.hello)

}

//cancel button
@objc func handleCancel(){
    self.dismiss(animated: true, completion: nil )
}

}

Solution

  • As you make the center x and y of the textfield with view not scrollView , You need

    textField.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      textField.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
      textField.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
      textField.widthAnchor.constraint(equalToConstant: view.frame.width - 64)
    ])
    

    Also remove

    view.addSubview(textField)
    self.view.addSubview(excerciseNumber)