我有一个视图控制器。基本上,我想检查一些条件,如果这些条件为真,我想手动设置它的大小类(覆盖它的默认大小类)。
为此,我必须覆盖它的UITraitCollection。我做了一些研究,并且能够找到这个功能:
setOverrideTraitCollection(collection: UITraitCollection!, forChildViewController childViewController: UIViewController!)
该函数允许我覆盖子视图控制器的特征集合,但我希望视图控制器能够覆盖它自己的特征。
所以回顾一下,我想做以下几点。加载视图控制器时,我希望它检查一些条件。如果它们是真的,我希望视图控制器将它的大小类重写为自定义类。
感谢您的帮助!
答案 0 :(得分:1)
您可以使用包装器视图控制器。实现'setOverrideTraitCollection(collection:UITraitCollection!,forChildViewController childViewController:UIViewController!)' 在其中,并将您的主视图控制器重构为该包装器的子项。
答案 1 :(得分:0)
我只是覆盖UIViewController
traitCollection
方法,效果很好!
// This is so we can get 'Regular' size class layout on an iPhone in landscape
- (UITraitCollection *)traitCollection
{
UITraitCollection *traitCollection = super.traitCollection;
UIDevice *device = [UIDevice currentDevice];
if (device.userInterfaceIdiom == UIUserInterfaceIdiomPhone && (device.orientation == UIDeviceOrientationLandscapeLeft || device.orientation == UIDeviceOrientationLandscapeRight)) {
if (traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
UITraitCollection *regularCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
traitCollection = [UITraitCollection traitCollectionWithTraitsFromCollections:@[traitCollection, regularCollection]];
}
}
return traitCollection;
}
答案 2 :(得分:-1)
这是它对我有用的方式(我正在寻找相同的iPad解决方案来区分肖像/风景模式而不使用childViewController):
TraitCollectionOverrideViewController.h
文件:
#import <UIKit/UIKit.h>
@interface TraitCollectionOverrideViewController : UIViewController
@end
TraitCollectionOverrideViewController.m
文件只有traitCollection
属性的getter方法:
- (UITraitCollection *)traitCollection {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
return super.traitCollection;
} else {
UITraitCollection *traitCollection_hCompact = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
UITraitCollection *traitCollection_vRegular = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];
UITraitCollection *traitCollection_CompactRegular = [UITraitCollection traitCollectionWithTraitsFromCollections:@[traitCollection_hCompact, traitCollection_vRegular]];
UITraitCollection *traitCollection_hRegular = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
UITraitCollection *traitCollection_RegularRegular = [UITraitCollection traitCollectionWithTraitsFromCollections:@[traitCollection_hRegular, traitCollection_vRegular]];
BOOL willTransitionToPortrait = self.view.frame.size.height > self.view.frame.size.width;
UITraitCollection *traitCollectionForOverride = willTransitionToPortrait ? traitCollection_CompactRegular : traitCollection_RegularRegular;
return traitCollectionForOverride;
}
}
我为iPad纵横模式使用Custom/Regular
尺寸等级以及iPad横向模式的标准Regular/Regular
尺寸等级进行了自定义
然后在我的真实视图控制器类中,我只使用此自定义类的子类:
@interface ForecastViewController : TraitCollectionOverrideViewController
然后你需要做的最后一部分 - 在故事板中设置与你需要的尺寸等级相关的约束。