Ios 如何以编程方式使用swift在UITextField下显示UITableView?

Ios 如何以编程方式使用swift在UITextField下显示UITableView?,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITextField,我想在它下面显示一个UITableView。我试图在UITableView中显示建议我的逻辑工作正常,但我想知道如何在UITextField下显示UITableView 目前,我正在做这样的事情 private func setUpAutoCompleteTable() { autocompleteTableView = UITableView(frame: CGRect(x: self.originTextField.bounds.minX,y

我有一个UITextField,我想在它下面显示一个UITableView。我试图在UITableView中显示建议我的逻辑工作正常,但我想知道如何在UITextField下显示UITableView

目前,我正在做这样的事情

 private func setUpAutoCompleteTable() {
        autocompleteTableView = UITableView(frame: CGRect(x: self.originTextField.bounds.minX,y: self.originTextField.bounds.maxY,width: self.originTextField.bounds.width,height: self.originTextField.bounds.height * 4), style: UITableView.Style.plain)
        self.view.addSubview(autocompleteTableView)
        
        autocompleteTableView.delegate = self
        autocompleteTableView.dataSource = self
        autocompleteTableView.isScrollEnabled = true
        autocompleteTableView.isHidden = true
        autocompleteTableView.register(LocationAutoCompleteCell.self, forCellReuseIdentifier: "AutoCompleteRowIdentifier")
    }
但它在屏幕顶部显示表格。我想告诉你,originTextField在另一个视图中

更新

这就是视图层次结构的外观


因此,要以编程方式布局tableView,需要在ViewController中声明它

var autoCompleteTableView: UITableView!
在ViewDidLoad中初始化它

autoCompleteTableView = UITableView(frame: .zero) // constraints will set the frame for us later
NSLayoutConstraint.activate([
    autoCompleteTableView.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 5), //the constant is how many pts below the textField you want the tableView to be
    autoCompleteTableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    autoCompleteTableView.widthAnchor.constraint(equalTo: view.widthAnchor),
    autoCompleteTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
在ViewDidLoad中设置约束

autoCompleteTableView = UITableView(frame: .zero) // constraints will set the frame for us later
NSLayoutConstraint.activate([
    autoCompleteTableView.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 5), //the constant is how many pts below the textField you want the tableView to be
    autoCompleteTableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    autoCompleteTableView.widthAnchor.constraint(equalTo: view.widthAnchor),
    autoCompleteTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])

然后可以设置需要设置的任何tableView属性。记住,VC还需要实现UITableViewDelegate和UITableViewDataSource,因此,要以编程方式布局tableView,需要在ViewController中声明它

var autoCompleteTableView: UITableView!
在ViewDidLoad中初始化它

autoCompleteTableView = UITableView(frame: .zero) // constraints will set the frame for us later
NSLayoutConstraint.activate([
    autoCompleteTableView.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 5), //the constant is how many pts below the textField you want the tableView to be
    autoCompleteTableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    autoCompleteTableView.widthAnchor.constraint(equalTo: view.widthAnchor),
    autoCompleteTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
在ViewDidLoad中设置约束

autoCompleteTableView = UITableView(frame: .zero) // constraints will set the frame for us later
NSLayoutConstraint.activate([
    autoCompleteTableView.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 5), //the constant is how many pts below the textField you want the tableView to be
    autoCompleteTableView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    autoCompleteTableView.widthAnchor.constraint(equalTo: view.widthAnchor),
    autoCompleteTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])

然后可以设置需要设置的任何tableView属性。请记住,您的VC还需要实现UITableViewDelegate和UITableViewDataSource

考虑到文本字段位于另一个视图中,您可能应该详细了解Gabe的答案

因此,您需要从情节提要中获取textfield的底部锚点引用。您可以通过控制将约束拖动到swift文件来实现这一点

然后你可以这样做:

autoCompleteTableView.topAnchor.constraint(equalTo: self.textFieldBottomAnchorReference, constant: 5)

考虑到textfield位于另一个视图中,您可能应该详细了解Gabe的答案

因此,您需要从情节提要中获取textfield的底部锚点引用。您可以通过控制将约束拖动到swift文件来实现这一点

然后你可以这样做:

autoCompleteTableView.topAnchor.constraint(equalTo: self.textFieldBottomAnchorReference, constant: 5)

您是在序列图像板中布局UI还是以编程方式布局UI?因此,我在序列图像板中完成了所有工作,只是表格视图是以编程方式进行的。表格视图是否也在序列图像板中布局,而您只是尝试在VIewController中修改它?或者您正在尝试完全以编程方式设置它?不,我正在尝试完全以编程方式设置它。哪个视图是originTextField?您需要参照外部视图。self.originTextField.bounds.maxY在originTextField的superView中为您提供了相对Y位置。您是在序列图像板中还是以编程方式布置UI?因此,我在序列图像板中完成了所有工作,只是表格视图是以编程方式布置的。序列图像板中是否也布置了表格视图,你只是想在VIewController中修改它?或者您正在尝试完全以编程方式设置它?不,我正在尝试完全以编程方式设置它。哪个视图是originTextField?您需要参照外部视图。self.originTextField.bounds.maxY提供了originTextField的superView中的相对Y位置。originTextField位于另一个视图中。如果它们不在同一视图中,约束将起作用?嘿!谢谢我知道你做了什么。你能看一下更新的问题吗?只要它们在同一个视图层次结构中就可以了,而且它们应该在同一个视图中,因为它们都在同一个viewcontrolleroriginTextField在另一个视图中。如果它们不在同一视图中,约束将起作用?嘿!谢谢我知道你做了什么。你能看一下更新后的问题吗?只要它们在同一个视图层次结构中就可以了,它们应该是,因为它们都在同一个viewcontrollerIt中,这很有意义,但我想在一组UITextFields下显示UITableView。这很有意义,但我想在一组UITextFields下显示UITableView。