iPhone X 屏幕适配

来源:互联网 发布:free mobile java动漫 编辑:程序博客网 时间:2024/05/17 03:39

今天升级xcode 9, 试了试iPhone X, 结果没有看到我预期的全屏效果,上下都没有显示全.

思忖片刻后想起我的启动图用的是LaunchImage, 而且只有四套, 没有5.8寸的,

注:如果用的是LaunchScreen.storyboard, 是不会因为缺少某个尺寸的启动图而显示不对的

然后我便生成了一张1125 × 2436 的启动图, 

但是不知道为何, 我的LaunchImag里面没有添加5.8寸的图片的位置,既然不能拖, 只好来硬的了,show in finder 直接把图片弄在LaunchImag文件下,并在Contents.json中images中添加

{
      "extent" : "full-screen",
      "idiom" : "iphone",
      "subtype" : "2436h",
      "filename" : "Default-812h@3x.png",
      "minimum-system-version" : "11.0",
      "orientation" : "portrait",
      "scale" : "3x"
    },

filename 取决于你自己起什么名了

然后再启动就OK了, 打印的屏幕大小为 375x812;

自动布局时需要注意一下几点  

1. iOS11 以前,我们布局时,视图的 top bottom一般参照的是 Top Layout Guide  Bottom Layout Guide

2. iOS11 以后,那两个参照已经 deprecated (过时)了,而被 Safe Area 

3. Safe Area 要求最低支持 iOS9.0 

顶部导航栏的高度是44+44, 底部有34的触摸区域, 效果如下


我们再计算导航栏高度的时候可以写一个宏

#define iphoneX                 ([UIScreen mainScreen].bounds.size.height>800.0f)#define kNavigationHeight       (iphoneX ? 88 : 64)

对于ScrollView ,iOS 11引入了contentInsetAdjustmentBehavior这个属性,我英文不好,就不翻译了
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));
使用的时候根据需求设置枚举值就行

我自己是这么用的,因为这样设置可以保证scrollView的内容,都显示在安全区

if (@available(iOS 11.0, *)) {        _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;    }




原创粉丝点击