我的目标是在 UITableView 中的节标题的右侧添加按钮。
但是,当我制作新目标iOS15时,我发现我必须采用UIButton配置来将padding设置为按钮中的添加图标。问题是,配置似乎没有“框架”属性,并将配置传递给按钮,然后将其框架构建设置为根本不显示按钮。
我的新的错误代码是:
func tableView (_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let frame = tableView.frame
let headerView = UIView(frame: CGRect(x:0, y: 0, width: frame.size.width, height: frame.size.height)) // create custom view
let sectionName = hasSections ? sectionTasks[section].sectionName : nil
if sectionName != nil {
let view = UIView()
let label = UILabel()
... // Label config, skipping here
let tableWidth = tableView.frame.width
label.frame = CGRect(x: 5, y: 0, width: tableWidth, height: 30)
headerView.addSubview(label)
}
// My faulty button
var buttonConfig = UIButton.Configuration.filled()
buttonConfig.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5)
buttonConfig.image = UIImage(named: "add_AsTemplate")
let xPos = tableView.frame.width-40-15
let button = UIButton(configuration: buttonConfig)
button.frame = CGRect(x: xPos, y: 0, width: 40, height: 40)
button.tag = section
button.addTarget(self, action: #selector(BaseTaskListVC.sectionNewTask_BtnPressed), for: .touchUpInside) // add selector called by clicking on the button
headerView.addSubview(button)
return headerView
}
我原来的按钮代码(跳过父函数的上下文):
let xPos = tableView.frame.width-40-15
let button = UIButton(frame: CGRect(x: xPos, y: 0, width: 40, height: 40))
button.tag = section
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5 )
button.setImage(UIImage(named: "add_AsTemplate"), for: UIControl.State.normal)
button.addTarget(self, action: #selector(BaseTaskListVC.sectionNewTask_BtnPressed), for: .touchUpInside)
headerView.addSubview(button)
我已经阅读了Apple文档,但它似乎没有明确解决这个问题。所有其他 SO 线程都展示了如何使用配置而不是配置+框架。
原代码:
采用新的配置方法:
您不需要使用“新”UIButton.Configuration
按钮样式...您可以继续使用“旧”按钮类。但是,无论哪种情况,您都应该使用自动布局/约束,而不是设置
.frame
。例如:
let button = UIButton()
var theImage: UIImage
if let img = UIImage(named: "add_AsTemplate") {
theImage = img
} else if let img = UIImage(systemName: "plus") {
// I don't have your "add_AsTemplate" image, so I'll use a SF Symbol
theImage = img
} else {
fatalError("Could not load a button image!")
}
button.setImage(theImage, for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5 )
button.translatesAutoresizingMaskIntoConstraints = false
headerView.addSubview(button)
NSLayoutConstraint.activate([
button.topAnchor.constraint(equalTo: headerView.topAnchor),
button.widthAnchor.constraint(equalToConstant: 40.0),
button.heightAnchor.constraint(equalToConstant: 40.0),
button.trailingAnchor.constraint(equalTo: headerView.trailingAnchor, constant: -15.0),
])
或者,使用 UIButton.Configuration
:
var buttonConfig = UIButton.Configuration.plain()
buttonConfig.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5)
var theImage: UIImage
if let img = UIImage(named: "add_AsTemplate") {
theImage = img
} else if let img = UIImage(systemName: "plus") {
// I don't have your "add_AsTemplate" image, so I'll use a SF Symbol
theImage = img
} else {
fatalError("Could not load a button image!")
}
buttonConfig.image = theImage
let button = UIButton(configuration: buttonConfig)
button.translatesAutoresizingMaskIntoConstraints = false
headerView.addSubview(button)
NSLayoutConstraint.activate([
button.topAnchor.constraint(equalTo: headerView.topAnchor),
button.widthAnchor.constraint(equalToConstant: 40.0),
button.heightAnchor.constraint(equalToConstant: 40.0),
button.trailingAnchor.constraint(equalTo: headerView.trailingAnchor, constant: -15.0),
])
无论实际宽度如何,都给出这个结果(黄色矩形“模拟”您的表格标题视图):