Я хочу поместить UIS-переключатель в раздел UITableView. Я уже сделал это, чтобы поместить сам коммутатор в раздел; но это не отвечает.
В titleForHeaderInSection
я загружаю вид как UITableViewHeaderFooterView
. После этого я вручную добавляю свои подпредставления, такие как UIImage, а также мой UISwitch:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let key = Array(indexNames.keys)[section];
view.translatesAutoresizingMaskIntoConstraints = false;
let header = view as! UITableViewHeaderFooterView;
header.isUserInteractionEnabled = true
if let imageResource = (indexNames[key]![0] as! SectionItem).getIconResource() {
let headerImageView = UIImageView(frame: CGRect(x: 8, // header.contentView.frame.origin.x,
y: header.frame.origin.y + header.bounds.height / 2 - 18, // header.contentView.frame.origin.y,
width: 36, height: 36))
let headerImage = UIImage(named: imageResource)
headerImageView.contentMode = .scaleAspectFit // OR .scaleAspectFill
headerImageView.image = headerImage;
headerImageView.clipsToBounds = true
header.contentView.addSubview(headerImageView);
let headerText = UILabel(frame: CGRect(x: 52, y: header.frame.origin.y + header.bounds.height / 2 - 10, width: 100, height: 20));
// headerText.translatesAutoresizingMaskIntoConstraints = false
headerText.textColor = UIColor.blue;
headerText.text = key;
header.contentView.addSubview(headerText);
let groupSwitch = UISwitch(frame: CGRect(x: header.bounds.width - 132, y: header.frame.origin.y + header.bounds.height / 2 - 10, width: 100, height: 20));
groupSwitch.isEnabled = true
groupSwitch.isUserInteractionEnabled = true
header.contentView.addSubview(groupSwitch)
header.contentView.bringSubview(toFront: groupSwitch)
header.contentView.isUserInteractionEnabled = true
// view.;
NSLayoutConstraint(item: headerImageView, attribute: .leading, relatedBy: .equal, toItem: header, attribute: .leading, multiplier: 1.0, constant: 16.0).isActive = true
NSLayoutConstraint(item: headerImageView, attribute: .top, relatedBy: .equal, toItem: header, attribute: .top, multiplier: 1.0, constant: 8).isActive = true;
NSLayoutConstraint(item: headerImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 24).isActive = true
NSLayoutConstraint(item: headerImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 24).isActive = true
NSLayoutConstraint(item: headerText, attribute: .leading, relatedBy: .equal, toItem: headerImageView, attribute: .trailing, multiplier: 1.0, constant: 16).isActive = true
NSLayoutConstraint(item: headerText, attribute: .top, relatedBy: .equal, toItem: header, attribute: .top, multiplier: 1.0, constant: 8).isActive = true;
NSLayoutConstraint(item: headerText, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 150.0).isActive = true
NSLayoutConstraint(item: headerText, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 20).isActive = true
}
}
Сам коммутатор обнаруживается, но не меняет своего состояния.
У вас есть идея, как решить мою проблему?