// // SettingsCell.swift // MullvadVPN // // Created by pronebird on 22/05/2019. // Copyright © 2019 Mullvad VPN AB. All rights reserved. // import UIKit class SettingsCell: UITableViewCell { let titleLabel = UILabel() let detailTitleLabel = UILabel() lazy var customDisclosureIndicator: UIImageView = { let disclosureImage = UIImage(named: "IconChevron")? .backport_withTintColor(UIColor.Cell.disclosureIndicatorColor, renderingMode: .alwaysOriginal) return UIImageView(image: disclosureImage) }() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) backgroundView = UIView() backgroundView?.backgroundColor = UIColor.Cell.backgroundColor selectedBackgroundView = UIView() selectedBackgroundView?.backgroundColor = UIColor.Cell.selectedAltBackgroundColor separatorInset = .zero backgroundColor = .clear contentView.backgroundColor = .clear titleLabel.translatesAutoresizingMaskIntoConstraints = false titleLabel.font = UIFont.systemFont(ofSize: 17) titleLabel.textColor = UIColor.Cell.titleTextColor detailTitleLabel.translatesAutoresizingMaskIntoConstraints = false detailTitleLabel.font = UIFont.systemFont(ofSize: 13) detailTitleLabel.textColor = UIColor.Cell.detailTextColor titleLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal) detailTitleLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) titleLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal) detailTitleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) contentView.addSubview(titleLabel) contentView.addSubview(detailTitleLabel) setLayoutMargins() NSLayoutConstraint.activate([ titleLabel.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor), titleLabel.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), titleLabel.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor), detailTitleLabel.leadingAnchor.constraint(greaterThanOrEqualToSystemSpacingAfter: titleLabel.trailingAnchor, multiplier: 1), detailTitleLabel.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), detailTitleLabel.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), detailTitleLabel.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor), ]) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func prepareForReuse() { super.prepareForReuse() setLayoutMargins() } override func layoutSubviews() { super.layoutSubviews() if #available(iOS 13, *) { // no-op } else { layoutSubviews_iOS12() } } private func setLayoutMargins() { // Set layout margins for standard acceessories added into the cell (reorder control, etc..) layoutMargins = UIMetrics.settingsCellLayoutMargins // Set layout margins for cell content contentView.layoutMargins = UIMetrics.settingsCellLayoutMargins } func setCustomDisclosureIndicator() { accessoryType = .none accessoryView = customDisclosureIndicator } func unsetCustomDisclosureIndicator() { accessoryView = nil accessoryType = .none } /// On iOS 12, standard edit and reorder controls do not respect layout margins. /// This method does layout adjustments to fix that. private func layoutSubviews_iOS12() { guard isEditing || showsReorderControl else { return } var leftOffset: CGFloat = 0 var rightOffset: CGFloat = 0 for subview in subviews { // Detect the edit control and move it, so that the nested image view is aligned along the left edge of the // layout margins. if subview.description.starts(with: "