0%

iOS 编程:适配 iOS 11 和 iPhone X

官方资源

WWDC 2017 Session 204: Updating Your App for iOS 11
  • 你可能需要为你的 APP 适配 iOS11
  • 简书 App 适配 iOS 11
  • iOS11 & iPhone X 适配指南
WWDC 视频: Designing for iPhone X
为 iPhone X 更新您的 app
Apple: Layout Guides and Safe Area

博客资源

  • GitHub: iOS11 适配系列教程
  • iOS 11 安全区域适配总结
  • 适配 iOS11,适配 iPhoneX,适配安全区的几个文章和宏
  • 美团点评:iPhone X 刘海打理指北
  • 贝聊科技:贝聊 iPhone X 适配实战
  • 手机管家 iPhoneX 的适配总结
  • 随手记在 iPhone X 上的适配实践总结
  • iOS 屏幕适配系列 (一): Autoresizing 技术
关于 iOS11 中 estimatedRowHeight
  1. estimatedRowHeight 是一个预估高度,iOS 11 之前为 0,在 iOS11 下默认为 44。
  2. 使用 estimatedRowHeight 属性,可以将系统计算 contentSize 的高度的几何计算成本从加载时推迟到滚动时再执行。
  3. 结论:当 cell 的实际高度大于预估高度的时候,会按照预估高度下的 cell 的数量来计算 contentSize ,当 cell 的实际高度小于预估高度的时候,会按照实际高度下的 cell 的数量来计算 contentSize。
  4. 使用 MJRefresh:(以下的问题在最新的版本已经修复了!)
    为什么使用 MJRefresh 在 iOS 11 下要设置 estimatedRowHeight=0?
    因为 MJRefresh 底部的上拉刷新是根据 contentSize 来计算的,当数据更新的时候,得出来的 contentSize 只是预估的。
  5. estimatedRowHeight 的值设置为 0,可以禁用此属性:
override func viewDidLoad() { 
tableView.estimatedRowHeight = 0 
tableView.estimatedSectionHeaderHeight = 0 
tableView.estimatedSectionFooterHeight = 0
}
天天品尝 iOS7 甜点 :: Day 20 :: View controller content and navigation bars
// iOS 11 中,`automaticallyAdjustsScrollViewInset` 属性被弃用了。
@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets;

该属性早年的作用:
默认为 YES,它允许容器视图控制器知道它应该调整插入此视图控制器视图中的滚动视图,以考虑状态栏,搜索栏,导航栏,工具栏或选项卡栏消耗的屏幕区域。

WWDC 204: Updating Your App for iOS 11 笔记

  • UIBarItem

横屏模式下,Tabbar 上的图标会变小,长按可以弹出显示大图标。

UIBarItem.landscapeImagePhone
UIBarItem.LargeContentSizeImaage
  • 控制导航栏显示大标题

主视图控制器上显示大图标

// 设置导航栏显示大标题
navigationBar.prefersLargeTitles = true

详细视图控制器只显示小图标

// 详细视图控制器中显示正常标题
navigationItem.largeTitleDisplayMode = .never
  • 导航栏自动集成 UISearchController
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = YES;
  • UIToolbarUINavigationBar 扩展了新的自动布局支持

自定义的 bar button items、自定义的 title 都可以通过 layout 来自定义尺寸。

Extensive new Auto Layout support

Keep your constraints inside your views

“We assume you know what you’re doing”

  • 避免零尺寸的自定义视图

UINavigationBar and UIToolbar provide position

You must provide size

  • Constraints for width and height
  • Implement intrinsicContentSize
  • Connect your subviews via constraints
  • Margins and Insets

margins 指的是【控件显示内容的边缘】和【控件本身边缘】之间的距离。

image

image

image

image

// systemMinimumLayoutMargins
viewRespectsSystemMinimumLayoutMargins = true // default
// directionalLayoutMargins

// edgesForExtendedLayout.top
// topLayoutGuide ❌
// edgesForExtendedLayout.bottom
// bottomLayoutGuide ❌
  • Safe Area

Describes area of view not occluded by ancestors —— 用于描述不被遮挡的视图区域

Available as insets or layout guide —— 可以用作插入量或者布局指引

Incorporates overscan compensation insets ——

image

image

  • Extending the Safe Area Insets—— 扩展安全区域插入量
// 通过 addtionalSafeAreaInsets 来改变 safeAreaInsets 的值
UIViewController.additionalSafeAreaInsets

// 获取改变的回调:
UIView.safeAreaInsetsDidChange()
UIViewController.viewSafeAreaInsetsDidChange()

适配代码

// 设置 view.top = self.view.top
CGFloat top;
if (@available(iOS 11.0, *)) {
    top = self.view.layoutMarginsGuide.layoutFrame.origin.y;
} else {
    top = 64;
}

// Masonry 框架
// 设置 View.top = self.view.top + 60
if (@available(iOS 11.0, *)) {
    make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).with.offset(60);
} else {
    make.top.equalTo(self.view.mas_top).with.offset(124); // 64 + 60
}
  • Scroll View

adjustedContentInset

typedef struct UIEdgeInsets {
    CGFloat top, left, bottom, right;  // 指定每个边的插入量(正数)。 值可能是负的(超出边缘)。
} UIEdgeInsets;

  • Table View—— 在 iOS 11 中默认启用 Self-Sizing

UITableViewAutomaticDimension

// 关闭 estimateRowHeight 属性
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

tableView.separatorInsetReference = .fromCellEdges // default

欢迎关注我的其它发布渠道