通用设备开发-iOS 9 分屏适配(监听SizeClass)

1.iPad分屏原则: 根据iPhone竖屏的展示样式进行展示

2.根控制器为SplitViewController的情况下,在iPhone竖屏上,默认会显示SplitViewController的主视图控制器

3.设置容器视图: 通过在SplitViewController的主视图控制器中添加一个容器视图,在主视图控制器上显示TabBarController的界面

1> 获取容器视图,当分屏时显示容器视图,当未分屏时,隐藏容器视图
2> 封装方法,通过分屏与否设置容器视图是否隐藏
//根据是否分屏来显隐容器视图
- (void)showContainerView:(BOOL)show{
    self.containerView.hidden = !show;
}

4.监听是否分屏:根据是否分屏来显隐容器视图

1> 如果判断是否分屏
如果当前设备为iPad,并且出现了iPhone竖屏的展示样式 说明正在分屏 -> 判断当前应用的界面展示样式(SizeClass) -> 如果SizeClass为 width compact&height regular 则说明正在分屏

2> 代码监听SizeClass
UIViewController和UIView类都遵守了""协议
该协议用于监听和获取SizeClass的情况

@protocol UITraitEnvironment 
@property (nonatomic, readonly) UITraitCollection *traitCollection NS_AVAILABLE_IOS(8_0);
/*! To be overridden as needed to provide custom behavior when the environment's traits change. */
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection NS_AVAILABLE_IOS(8_0);
@end

通过UITraitCollection, 就可以拿到SizeClass信息:

NS_CLASS_AVAILABLE_IOS(8_0) @interface UITraitCollection : NSObject 

- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

- (BOOL)containsTraitsInCollection:(nullable UITraitCollection *)trait;

/*! Returns a trait collection by merging the traits in `traitCollections`. The last trait along any given
 axis (e.g. interface usage) will supercede any others. */
+ (UITraitCollection *)traitCollectionWithTraitsFromCollections:(NSArray *)traitCollections;

+ (UITraitCollection *)traitCollectionWithUserInterfaceIdiom:(UIUserInterfaceIdiom)idiom;
@property (nonatomic, readonly) UIUserInterfaceIdiom userInterfaceIdiom; // unspecified: UIUserInterfaceIdiomUnspecified

+ (UITraitCollection *)traitCollectionWithDisplayScale:(CGFloat)scale;
@property (nonatomic, readonly) CGFloat displayScale; // unspecified: 0.0

+ (UITraitCollection *)traitCollectionWithHorizontalSizeClass:(UIUserInterfaceSizeClass)horizontalSizeClass;
@property (nonatomic, readonly) UIUserInterfaceSizeClass horizontalSizeClass; // unspecified: UIUserInterfaceSizeClassUnspecified

+ (UITraitCollection *)traitCollectionWithVerticalSizeClass:(UIUserInterfaceSizeClass)verticalSizeClass;
@property (nonatomic, readonly) UIUserInterfaceSizeClass verticalSizeClass; // unspecified: UIUserInterfaceSizeClassUnspecified

+ (UITraitCollection *)traitCollectionWithForceTouchCapability:(UIForceTouchCapability)capability NS_AVAILABLE_IOS(9_0);
@property (nonatomic, readonly) UIForceTouchCapability forceTouchCapability NS_AVAILABLE_IOS(9_0); // unspecified: UIForceTouchCapabilityUnknown

@end

3> 监听时__ "必须监听SplitViewController的SizeClass"__,因为分屏时,整体界面(SplitVc)的SizeClass发生了变化,而主视图控制器的界面(SizeClass)没有发生变化

将主视图控制器中封装的容器视图显示与否的方法提到.h中进行声明,通过SplitViewController监听视图是否分屏,设置主视图的容器视图显示与否

#pragma mark -- UITraitEnvironment

// 当SizeClass发生变化后调用
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
    
    // 判断当前的SizeClass,如果为width compact&height regular 则说明正在分屏
    BOOL isTrait = (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) && (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular);
    
    if (isTrait) {
        // 正在分屏
        NSLog(@"正在分屏");
        JSMasterViewController *masterViewController = self.viewControllers[0];
        [masterViewController showContainerView:isTrait];
        
    }else {
        
        NSLog(@"没有分屏");
        JSMasterViewController *masterViewController = self.viewControllers[0];
        [masterViewController showContainerView:isTrait];
    }
    
}

未分屏:(主视图中的容器视图隐藏)

通用设备开发-iOS 9 分屏适配(监听SizeClass)_第1张图片
未分屏.png

分屏:(主视图中的容器视图显示)

通用设备开发-iOS 9 分屏适配(监听SizeClass)_第2张图片
分屏.png

你可能感兴趣的:(通用设备开发-iOS 9 分屏适配(监听SizeClass))