【发布时间】:2020-12-31 17:18:18
【问题描述】:
我第一次尝试将页脚放置在正确的位置,但是它太靠近前导边距,这让我添加了 1 个约束来解决问题。这样做会导致页脚高于页眉,这显然不是预期的行为。我想做的就是从我的第一次尝试中增加 10 分的领先优势。想法?
第一次尝试:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section == 0 {
let label = UILabel()
label.text = "Your birth month is not set"
label.font = UIFont.preferredFont(forTextStyle: .footnote)
label.textColor = UIColor.gray
return label
}
return UIView()
}
第二次尝试:
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section == 0 {
let label = UILabel()
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
let constraints = label.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: 10)
NSLayoutConstraint.activate([constraints])
label.text = "Your birth month is not set"
label.font = UIFont.preferredFont(forTextStyle: .footnote)
label.textColor = UIColor.gray
return label
}
return UIView()
}
【问题讨论】:
-
您不能只添加一个约束。一旦你决定使用任何约束,你必须添加足够的约束来完全定位标签和足够的约束来完全设置视图的高度。
-
另外
view.addSubview(label)是错误的。提供视图由您决定。
标签: ios swift uitableview footer