[英]UITableview Add Padding to custom section header imageview
我试图将填充添加到此自定义标题视图(图像)。
该表是static content cells
,我仅将图像添加到第一部分。
我试图遵循此链接,但未成功。
我试图在标题视图中添加一些顶部和底部填充。
我尝试这样做没有运气:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
var header = tableView.tableHeaderView
let image = UIImage(named: "Placeholder") // is 121x121
let imageView = UIImageView(frame: CGRect(x: 0, y: 20, width: tableView.frame.width, height: tableView.sectionHeaderHeight))
imageView.image = image
imageView.contentMode = .scaleAspectFit
header = imageView
return header
}
return nil
}
我也在委托中定义标题高度:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 125.0
}
return 44.0
}
结果是相同的,没有填充。
任何帮助,将不胜感激
我还尝试将imageView添加到标题子视图中,这导致整个图像消失。
不要将UIImageView作为标题返回,而应将它作为子视图添加到UIView,然后在其中创建填充。
您的问题是,即使为标题设置了框架,UITableView也会调整其大小以适合其宽度以及您设置为125的高度。因此,您必须将其包含在将填充表格标题的UIView中,并包含您希望为其填充的UIImageView。
编辑:试试这个
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let header = UIView()
header.backgroundColor = UIColor.clear
let image = UIImage(named: "Placeholder") // is 121x121
let imageView = UIImageView(frame: CGRect(x: 0, y: 20, width: tableView.frame.width, height: tableView.sectionHeaderHeight))
imageView.image = image
imageView.contentMode = .scaleAspectFit
header.addSubview(imageView)
return header
}
return nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.