【问题标题】:TableView footer not in correct locationTableView 页脚位置不正确
【发布时间】: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


【解决方案1】:

尝试输入此代码。根据您的要求进行框架更换。

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let headerView = UIView(CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
    let lblDate = UILabel(frame: CGRect(x: 10, y: 0, width: headerView.frame.width - 10, height: 50))
    headerView.addSubview(lblDate)
    lblDate.text = "text"
    return headerView
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 50
}

【讨论】:

    猜你喜欢
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多