IBDesignables and traitCollection in live renderin

2019-02-23 12:18发布

问题:

I am building my custom UIControl, a custom button built as an IBDesignable, which needs to change based on the size class in which it is being displayed. I have a method -setupForTraitCollection, which looks like this:

func setupForTraitCollection() {
   switch(traitCollection.horizontalSizeClass, traitCollection.verticalSizeClass) {
   case (.Regular, _):
       // iPad - not compressed design
       compressed = false
   default:
       // iPhone - compressed design
       compressed = true
   }
}

This code works great when compiled, but in live rendering, and when debugging the view, it never hits the "iPad" switch case. I am starting to give up here and simply accept that traitCollections aren't available in live rendering, but I'd like to have this confirmed. Better still, if someone could point me in the direction of finding a solution.

So the to-the-point question is - Can I use traitCollections in an IBDesignable and if so, how?

I'd really like to be able to change size class in IB and see the result on my custom control.

回答1:

Interface Builder does not yet set the trait collection for designable views when we are rendering in Xcode. We are tracking this with radar://17278773. Filing a report at http://bugreport.apple.com and mentioning that bug ID will help us track demand and prioritize appropriately.