iosswiftstylinguiappearance

UIAppearance edge insets


I have a UIView subclass which I use in my app (And is in a 3rd party library) for displaying toast notifications. I want to modify this slightly for another app I'm working on and so I'm adding styling properties via UIAppearance. I have some working, but some of them refuse to work properly!

/// The visible duration of the toast view
@objc public dynamic var visibleDuration: CGFloat = 2.0

/// The margins to apply around the toast view
@objc public dynamic var margins: UIEdgeInsets = .zero

visibleDuration works fine here, and is set by ToastView.appearance().visibleDuration = 6 but margins is never non-zero! Is there something special I need to do here to support setting a UIEdgeInsets via UIAppearance?


Solution

  • It turns out this was being correctly set, but due to the nature of my class the point at which I was trying to lay out my view using them they weren't available as their UIAppearance values. It seems like the view needs to be added to the window hierarchy before the values are updated to reflect their UIAppearance values! Moving around my layout code to occur after adding to the window fixed this!