Accessing UIViewcontroller.traitCollection compile

2019-08-24 03:42发布

问题:

My storyboard is build in xcode6 and is using the new constraint based layouts. My app runs in two configurations, wRegular hRegular and wCompact hAny. Any time I try to access the traitCollection in code I get the following error:

[MYUIControllerName traitCollection]: unrecognized selector sent to instance.

I have also tried implementing the following function, but it never gets called.

-(void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

回答1:

I don't think it's exactly right that size classes don't work < iOS 8. https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/DeployingSizeClassesonEarlieriOSVersions.html#//apple_ref/doc/uid/TP40014436-CH13-SW1



回答2:

I got the same issue. The answer is use trait collection only in viewWillAppear and willTransitionToTraitCollection. Theses are the only method when you can be sure they are set and correctly set. In iOS8 it can not crash but your trait collection will be unspecified.