Я пытаюсь создать компоненты с собственным стилем, которые могут обрабатывать пользовательские свойства, передаваемые сверху, и соответствующим образом корректировать их стиль. Например:
Код: Выделить всё
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.registerForTraitChanges([DangerStyleTrait.self]) { (self: Self, _) in
// Do some styling changes if danger is true or not
}
}
}
Код: Выделить всё
class InfoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = CustomButton()
view.addSubview(button)
//
}
}
Код: Выделить всё
func showWarningAlert() {
let viewController = InfoViewController
viewController.traitOverrides.dangerStyleEnabled = true
present(viewController, animated: true)
}
If this is expected behaviour, is there a workaround for the above scenario? I would hope it is a reasonable use case to want to set up a whole view controller and then later apply a trait and have it propagate all the way down on presentation.
Thanks!
Источник: https://stackoverflow.com/questions/781 ... -hierarchy
Мобильная версия