我有以下traitCollectionDidChange
。
- (void) traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
[super traitCollectionDidChange:previousTraitCollection];
if (previousTraitCollection.horizontalSizeClass != self.traitCollection.horizontalSizeClass){
NSInteger horizontalClass = self.traitCollection.horizontalSizeClass;
NSInteger verticalClass = self.traitCollection.verticalSizeClass;
switch(horizontalClass){
case UIUserInterfaceSizeClassCompact:
if (verticalClass == UIUserInterfaceSizeClassRegular){
// Setup for iPhone
}
break;
case UIUserInterfaceSizeClassRegular:
if (verticalClass == UIUserInterfaceSizeClassRegular){
// Setup for iPad
}
break;
default:
break;
}
}
}
Update
:即使只是转动iPad也会失败。无论何时再次调用该方法,它都会失败:if (previousTraitCollection.horizontalSizeClass != self.traitCollection.horizontalSizeClass){
发出错误:[NSConcreteNotification horizontalSizeClass]:unrecognized selector sent to instance
。由于previousTraitCollection
为nil
。
你知道为什么它第二次失败了吗?