ios - 如何使用 NSLayoutConstraint 以编程方式使容器中的表页脚 View 居中?

标签 ios swift nslayoutconstraint

我正在为一个有收藏夹的应用程序构建一个屏幕。该屏幕将显示用户收藏的项目列表。该列表将显示在表格 View 中,非常普通的东西。

当尚未添加收藏夹时,我正在尝试为列表的空状态添加一些代码。我想将 UIView 放在 tableFooter 中,然后将 UILabel 放在该 View 的中心。我希望 View 占据屏幕内的所有可用空间。

到目前为止,这就是我所拥有的:

self.tableView.tableFooterView = buildTableViewFooter()

非常不言自明。

func buildTableViewFooter() -> UIView {
  let footer = UIView(frame: self.tableView.frame)
  let label = UILabel()
  label.text = "Use the heart icon to add favorites"
  label.font = UIFont.italicSystemFont(ofSize: 21.0)
  label.textColor = UIColor.lightGray
  label.textAlignment = .center
  label.numberOfLines = 0
  let centerHorizontalConstraint = NSLayoutConstraint(
      item: label,
      attribute: .centerX,
      relatedBy: .equal,
      toItem: footer,
      attribute: .centerX,
      multiplier: 1,
      constant: 0
  )
  footer.addSubview(label)
  footer.addConstraints([
      centerHorizontalConstraint,
  ])
  return footer
}

当我运行应用程序时,我可以看出我的方法正在触发并且 View 已成功创建。但是,我在屏幕上的任何地方都看不到文字。 LayoutConstraints 引擎试图提供帮助,但我不太确定它想告诉我什么:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x60400028a9b0 h=--& v=--& UILabel:0x7fa8565417d0'Use the heart icon to add...'.midX == 0   (active)>",
    "<NSLayoutConstraint:0x604000290f90 UILabel:0x7fa8565417d0'Use the heart icon to add...'.centerX == UIView:0x7fa856514e60.centerX   (active)>",
    "<NSAutoresizingMaskLayoutConstraint:0x6040002931f0 h=--& v=--& 'UIView-Encapsulated-Layout-Left' UIView:0x7fa856514e60.minX == 0   (active, names: '|':UITableView:0x7fa857889a00 )>",
    "<NSLayoutConstraint:0x60400028a2d0 'UIView-Encapsulated-Layout-Width' UIView:0x7fa856514e60.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x604000290f90 UILabel:0x7fa8565417d0'Use the heart icon to add...'.centerX == UIView:0x7fa856514e60.centerX   (active)>

非常感谢!

最佳答案

您还需要添加 y 约束,例如

let topConstraint = NSLayoutConstraint(
  item: label,
  attribute: .top,
  relatedBy: .equal,
  toItem: footer,
  attribute: .top,
  multiplier: 1,
  constant: 10
)

并将其设置为标签

label.translatesAutoresizingMaskIntoConstraints = false

关于ios - 如何使用 NSLayoutConstraint 以编程方式使容器中的表页脚 View 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51126799/

相关文章:

ios - 在 View Controller 之间传递数据

ios - swift:如何从两个单独的方法执行相同的闭包

ios - 尝试使用 NSUbiquitousKeyValueStore 使用 iCloud 做一些非常简单的事情

ios - Realm 关系的谓词,检查列表是否为空

swift - 如何以编程方式设置控制 ImageView 宽度的 slider

ios - 从斯坦福 iOS 类(class)中收到错误,但我的代码似乎相同

ios - Swift iOS - 如何实现多行不同字号的SegmentedControl

ios - 无法找到在 Swift 中使用 Google 客户端库的用户的 serverAuthCode

ios - 在特定条件下切换约束

ios - 是否可以定义与未知 UIView 相关的自动布局约束?