textField 不会在可编程滚动视图中移动

textField will not move in programable scrollview

我有一个 UIScrollView。我有一个 UITextField 和一个 UILabel。出于某种原因,我的 UITextField 它不会在我的滚动视图中移动,但我的 UILablel 会。我添加 UITextField 的方式与使用 scrollView.addSubview(textField) 将 UILabel 添加到滚动视图的方式相同。有人看到我在这方面做错了什么吗?

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

}

当您使用 view 而不是 scrollView 制作文本字段的中心 x 和 y 时,您需要

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

也删除

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