蝴蝶不菲
iPad具有水平和垂直尺寸的“常规”尺寸特性,不区分纵向和横向。可以UIViewController通过方法在自定义子类代码中覆盖这些大小特征traitCollection,例如:- (UITraitCollection *)traitCollection {
// Distinguish portrait and landscape size traits for iPad, similar to iPhone 7 Plus.
// Be aware that `traitCollection` documentation advises against overriding it.
UITraitCollection *superTraits = [super traitCollection];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UITraitCollection *horizontalRegular = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
UITraitCollection *verticalRegular = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassRegular];
UITraitCollection *regular = [UITraitCollection traitCollectionWithTraitsFromCollections:@[horizontalRegular, verticalRegular]];
if ([superTraits containsTraitsInCollection:regular]) {
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
// iPad in portrait orientation
UITraitCollection *horizontalCompact = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
return [UITraitCollection traitCollectionWithTraitsFromCollections:@[superTraits, horizontalCompact, verticalRegular]];
} else {
// iPad in landscape orientation
UITraitCollection *verticalCompact = [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassCompact];
return [UITraitCollection traitCollectionWithTraitsFromCollections:@[superTraits, horizontalRegular, verticalCompact]];
}
}
}
return superTraits;}- (BOOL)prefersStatusBarHidden {
// Override to negate this documented special case, and avoid erratic hiding of status bar in conjunction with `traitCollection` override:
// For apps linked against iOS 8 or later, this method returns true if the view controller is in a vertically compact environment.
return NO;}这使iPad具有与iPhone 7 Plus相同的尺寸特性。请注意,其他iPhone型号通常具有“紧凑宽度”特征(而不是常规宽度),无论方向如何。以这种方式模仿iPhone 7 Plus允许该模型在Xcode的Interface Builder中用作iPad的替身,它不知道代码中的自定义。请注意,iPad上的分割视图可能会使用与普通全屏操作不同的大小特征。这个答案是基于这篇博客文章中采用的方法,并做了一些改进。更新以修复iPad版本中的间歇性隐藏状态栏,以及潜在的(更新)特征的践踏UITraitCollection。还注意到Apple文档实际上建议不要覆盖traitCollection,因此将来可能会出现这种技术的问题。