如果您想了解如何在 iOS 13 中实现暗模式,我建议您观看这个wwdc 视频。但是,如果您只是想快速了解调用方法的时间,这里有一个总结:
layoutSubviews()
:layoutSubviews方法用于UIView
子类。您可以在自己的UIView
子类中覆盖它,如下所示:
class CustomCollectionView: UICollectionView {
override func layoutSubviews() {
super.layoutSubviews()
// your implementation here
}
}
traitCollectionDidChange()
: traitCollectionDidChange方法可以UIViewController
被子类覆盖。您可以在自己的UIViewController
子类中覆盖它,如下所示:
class CustomViewController: UIViewController {
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
// your implementation here.
}
}
如果您想特别知道主题何时发生变化,那么您的实现traitCollectionDidChange
将如下所示:
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13, *), traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
// handle theme change here.
}
}