Hi,
I have created a custom UIContentConfiguration + UIContentView for a TableView and using the custom Configuration and the default Configuration from UIListContentConfiguration in a same TableView (Style insetGrouped). But the separators from the custom configuration are different than that from the default configuration. And I didn't see an option to modify this through the custom configuration. Do anybody know how this is done with UIListContentConfiguration? For me currently the only option seems to be using "cell.separatorInset", but for the UIListContentConfiguration I don't need this.
In the image below you see the problem. First, third and fifth cell is default configuration (UIListContentConfiguration.valueCell()). Second and fourth is the custom configuration. And under the custom configuration cells the separator is beginning at zero. This can be fixed with: cell.separatorInset.left = 20
.
This one is an example without image, but if I add an image for the default configuration, the text is much more behind and the fix with cell.separatorInset.left = 20
is not working in this case.
For default configuration:
let cell = tableView.dequeueReusableCell(withIdentifier: "default", for: indexPath)
var config = UIListContentConfiguration.valueCell()
config.text = "Test"
config.secondaryText = "Test"
cell.contentConfiguration = config
return cell
For custom configuration:
Configuration + ContentView:
struct SwitchConfiguration: UIContentConfiguration {
var text: String?
var value: Bool
var delegate: SwitchDelegate?
func makeContentView() -> UIView & UIContentView {
return SwitchContentView(configuration: self)
}
func updated(for state: UIConfigurationState) -> SwitchConfiguration {
return self
}
}
class SwitchContentView: UIView, UIContentView {
var configuration: UIContentConfiguration {
didSet {
self.configure()
}
}
private let textLabel: UILabel = UILabel()
private let `switch`: UISwitch = UISwitch()
init(configuration: UIContentConfiguration) {
self.configuration = configuration
super.init(frame: .zero)
self.switch.addTarget(self, action: #selector(self.switchValueDidChange(_:)), for: .valueChanged)
self.addSubview(self.textLabel)
self.addSubview(self.switch)
self.directionalLayoutMargins = .init(top: 8, leading: 20, bottom: 8, trailing: 8)
self.textLabel.translatesAutoresizingMaskIntoConstraints = false
self.textLabel.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor).isActive = true
self.textLabel.centerYAnchor.constraint(equalTo: self.layoutMarginsGuide.centerYAnchor).isActive = true
self.switch.translatesAutoresizingMaskIntoConstraints = false
self.switch.trailingAnchor.constraint(equalTo: self.layoutMarginsGuide.trailingAnchor, constant: -16).isActive = true
self.switch.centerYAnchor.constraint(equalTo: self.layoutMarginsGuide.centerYAnchor).isActive = true
self.switch.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor).isActive = true
self.switch.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor).isActive = true
self.configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func configure() {
guard let config = self.configuration as? SwitchConfiguration else {
return
}
self.textLabel.text = config.text
self.switch.isOn = config.value
}
@IBAction
func switchValueDidChange(_ switch: UISwitch) {
guard let config = self.configuration as? SwitchConfiguration else {
return
}
config.delegate?.switchValueDidChange(`switch`.isOn)
}
}
Cell:
let cell = tableView.dequeueReusableCell(withIdentifier: "custom", for: indexPath)
var config = SwitchConfiguration()
config.text = "Test"
config.value = true
cell.contentConfiguration = config
//cell.separatorInset.left = 20
return cell