如何在 viewforheaderinsection 中添加标题?
How can I add titile in viewforheadersection?
这是完整的代码。
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
}
如何在 header 中添加标题?
还有,还有另一种设置图像自动布局的方法吗?
试试这个:
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
将 UILabel 添加到您的自定义 header 视图
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
}
这是完整的代码。
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
}
如何在 header 中添加标题?
还有,还有另一种设置图像自动布局的方法吗?
试试这个:
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
将 UILabel 添加到您的自定义 header 视图
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
}