【问题标题】:How can I add titile in viewforheadersection?如何在 viewforheaderinsection 中添加标题?
【发布时间】:2019-11-25 06:14:27
【问题描述】:

这是完整的代码。

let sections: [String] = ["Cleaning","Computer Repair", "Electircity", "Painting", "Plumbing"]

let sectionImages: [UIImage] = [picutre1, picture2, picture3, picture4]

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let imageView = UIImageView()
    imageView.image = sectionImages[section]
    let headerView = UIView()
    headerView.addSubview(imageView)

    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.centerXAnchor.constraint(equalTo: headerView.centerXAnchor).isActive = true
    imageView.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true   
    imageView.heightAnchor.constraint(equalToConstant: 60).isActive = true
    imageView.widthAnchor.constraint(equalToConstant: 60).isActive = true

    return headerView
}

如何在标题中添加标题?

还有,还有另一种设置图像自动布局的方法吗?

【问题讨论】:

  • 您可以通过在xib 中添加视图来制作自定义标题。

标签: ios swift xcode header title


【解决方案1】:

试试这个:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let imageView = UIImageView()
        imageView.image = sectionImages[section]

        let titleLabel = UILabel()
        titleLabel.text = sections[section]
        titleLabel.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        titleLabel.textAlignment = .left

        let headerView = UIView()
        headerView.addSubview(imageView)
        headerView.addSubview(titleLabel)

        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.leadingAnchor.constraint(equalTo: headerView.leadingAnchor, constant: 20).isActive = true
        imageView.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: 60).isActive = true
        imageView.widthAnchor.constraint(equalToConstant: 60).isActive = true

        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 20).isActive = true
        titleLabel.trailingAnchor.constraint(equalTo: headerView.trailingAnchor, constant: -20).isActive = true
        titleLabel.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true

        return headerView
    }

screenshot

【讨论】:

  • 代码可以运行,但我有一个小问题。图片和文字居中如何向左移
  • 我试过这段代码。它在我的项目中运行良好。可以加个截图吗?
【解决方案2】:

将 UILabel 添加到您的自定义标题视图中

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let imageView = UIImageView()
    imageView.image = sectionImages[section]
    let headerView = UIView()
    headerView.addSubview(imageView)

    //Title
    var yourLabel: UILabel = UILabel()
    yourLabel.frame = CGRectMake(0, 0, 200, 20) // Change the frame based on your needs
    yourLabel.text = "test label"
    headerView.addSubview(yourLabel)

    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.centerXAnchor.constraint(equalTo: headerView.centerXAnchor).isActive = true
    imageView.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true   
    imageView.heightAnchor.constraint(equalToConstant: 60).isActive = true
    imageView.widthAnchor.constraint(equalToConstant: 60).isActive = true

    return headerView
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 2023-03-24
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多