swift - 在 UIView 内部设置 UITableView 的约束

标签 swift uitableview autolayout swift3 programmatically-created

我有一个 UIViewController,它有一些基本控件,并且在中心有一个 UIView (作为可交换 ui 控件的容器)。我根据用户想要执行的操作替换此中心 UIView(容器)中的 UI 控件。进入 UIView 容器的所有 UI 控件都是以编程方式定义的,我使用编程约束将它们放置在 UIView 容器中。

这对于我迄今为止完成的所有 UI 控件集都适用。现在我正在向包含 UITableView 的 UIView 容器添加一组控件

我不知道如何以编程方式让 TableView 显示在 UIView 中。我可以定义一个按钮和标签并运行应用程序并查看带有按钮和标签的容器。如果我如下添加 UITableView 那么容器根本不会显示。

// tableView
tableView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: inputsContainerView.bottomAnchor).isActive = true
tableView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor).isActive = true
tableView.heightAnchor.constraint(equalTo: inputsContainerView.heightAnchor, multiplier: 1/2).isActive = true

在上面的代码之前,我已经将所有需要的控件添加到容器 subview 中......

// Add controls to the view
inputsContainerView.addSubview(listTextView)
inputsContainerView.addSubview(listImageButton)
inputsContainerView.addSubview(listImageClear)
inputsContainerView.addSubview(tableView)

如果我离开表格 View ,那么容器将显示其他三个字段。如果我添加表格 View ,那么容器和所有其他三个控件都会消失。

如何将 tableView 添加到 UIView 并使其显示?

这是我定义 UITable View 的方式

let tableView: UITableView =  {
    let tv = UITableView()
    return tv
}()

作为比较,当我像这样定义其他控件时,它们在添加到 subview 并以编程方式设置约束后显示得很好

例如

// DEFINE
let listTextView: UITextView = {
    let textView = UITextView()
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.text = ""
    textView.textColor = defaultTextColor
    textView.font = subtitleFont
    textView.layer.borderColor = UIColor.black.cgColor
    textView.layer.borderWidth = 1
    textView.textAlignment = NSTextAlignment.left
    return textView
}()

然后稍后

// Place - with constraints
// listTextView
listTextView.leftAnchor.constraint(equalTo: inputsContainerView.leftAnchor, constant: padFromLeft).isActive = true
listTextView.topAnchor.constraint(equalTo: inputsContainerView.topAnchor, constant: 10).isActive = true
listTextView.widthAnchor.constraint(equalTo: inputsContainerView.widthAnchor, multiplier: 9/16).isActive = true
listTextView.heightAnchor.constraint(equalTo: inputsContainerView.widthAnchor, multiplier: 5/16).isActive = true

最佳答案

刚刚添加我的评论作为详细答案,以便其他人可以更快地看到解决方案并从中受益。

取自apple documentation ,要以编程方式设置您自己的约束,您需要将 translatesAutoresizingMaskIntoConstraints 设置为 false:

Note that the autoresizing mask constraints fully specify the view’s size and position; therefore, you cannot add additional constraints to modify this size or position without introducing conflicts. If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to false, and then provide a non ambiguous, nonconflicting set of constraints for the view.

因此,在您的情况下,当您定义它时,您会错过为 TableView 设置它。只需添加这一行即可:

tv.translatesAutoresizingMaskIntoConstraints = false

关于swift - 在 UIView 内部设置 UITableView 的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519013/

相关文章:

ios - 如何让ScrollView即使滚动到最后也消耗触摸事件?

iOS7 UITableView 滑动删除和触摸隐藏删除按钮

ios - 使用自动布局创建的 UIView 的左右 16 像素边距

swift - 如何从 UIPickerView 更新自定义 TableViewCell 中的 textfield.text 值

ios - 单击注释时从 map View 进行转场

ios - 使用 SceneKit 从 CapturedRoom.walls 重新创建 RoomPlan

ios - 从 URL 加载图像会增加 iOS 中单元格的大小

ios - UITableView 删除按钮不会对手势使用react

ios - UICollectionView 自动调整大小会导致错误

ios - 您应该向 Stackview 的 subview 添加约束吗?