美文网首页
iOS11和iPhone X适配

iOS11和iPhone X适配

作者: CoderJay | 来源:发表于2017-09-27 12:55 被阅读0次

在iOS11中,系统对automaticallyAdjustsScrollViewInsets已经废弃,所以导致之前使用self.automaticallyAdjustsScrollViewInsets = NO;对tableView调整,失效,tableView整体上移了20pt

如图:

iOS11对ScrollView新增 UIScrollViewContentInsetAdjustmentBehavior

官方Api

typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {

UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable

UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)

UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted

UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets

} API_AVAILABLE(ios(11.0),tvos(11.0));

解决方案

if (@available(iOS 11.0, *)) {

self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

}else{

self.automaticallyAdjustsScrollViewInsets = NO;

}

至此,tableView上移的问题解决。

关于iPhone X适配

由于目前iPhone X还没有发售,所以,笔者知识用模拟器来对iPhone X进行适配,但过程遇到了一些问题:

首先,让我们了解一下iPhone X屏幕的一些新特性

关于iPhone X的设计,看看苹果开发者网站给我们的介绍

https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/

在iPhone X之前,我们通常将状态栏和导航高度之和设置为一个常量宏

#define TOPBAR_HEIGHT 64

然而,iPhone X的状态栏高度并非20,而是44,也就是状态栏高度于导航栏高度之和为88,所以之前的宏定义对于iPhone X并不适用,那么解决这个适配问题就很容易了,

1,是判断手机型号是否为iPhone X,可能有人会想,直接[[UIDevice currentDevice] model]获取到手机型号,但是目前并没有iPhone X真机,而模拟器也只能获取到iPhone Simulator  ,

所以这个判断方式并不可行,那么,就只能根据屏幕的分辨率来判断了(但是这个就要区分是横屏还是竖屏)难免还是有点麻烦

2,笔者目前采用的就是这个方式,那就是直接获取状态栏高度与导航的高度之和,那也就是我们可以修改一下我们的宏定义为:#define TOPBAR_HEIGHT [UIApplication sharedApplication].statusBarFrame.size.height + 44,经过我的尝试,完美解决,当然这种改动也是最小的。

之后还有关于iPhone X相关的适配问题后续抽空再分享。

相关文章

网友评论

      本文标题:iOS11和iPhone X适配

      本文链接:https://www.haomeiwen.com/subject/oriyextx.html