Skip to content

Instantly share code, notes, and snippets.

@Koze

Koze/TableViewCell.swift

Last active Mar 6, 2020
Embed
What would you like to do?
Accessible TableViewCell like system UITableViewCell
//
// TableViewCell.swift
// iOS12App
//
// Created by Kazuma Koze on 2020/03/05.
// Copyright © 2020 Climb App. All rights reserved.
//
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subheadLabel: UILabel!
@IBOutlet weak var bodyLabel: UILabel!
@IBOutlet weak var stackView: UIStackView!
override func awakeFromNib() {
super.awakeFromNib()
// for SwiftUI preview
setContentHuggingPriority(.defaultHigh, for: .vertical)
updateStackViewAxis(traitCollection.preferredContentSizeCategory.isAccessibilityCategory)
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
let isAccessibilityCategory = traitCollection.preferredContentSizeCategory.isAccessibilityCategory
if isAccessibilityCategory != previousTraitCollection?.preferredContentSizeCategory.isAccessibilityCategory {
updateStackViewAxis(isAccessibilityCategory)
}
}
private func updateStackViewAxis(_ isAccessibilityCategory: Bool) {
stackView.axis = isAccessibilityCategory ? .vertical : .horizontal
stackView.alignment = isAccessibilityCategory ? .leading : .center
}
// for SwiftUI preview
override var intrinsicContentSize: CGSize {
return systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.