启动页的适配
UIScrollView适配
iOS 11废除了automaticallyAdjustsScrollViewInsets这个属性,转而使用了UIScrollView的contentInsetAdjustmentBehavior来替代。
- AppDelegate中全局设置
if (@available(iOS 11.0, *))
{
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
- WKWebView 需要再单独设置(原因未知)
if (@available(iOS 11.0, *))
{
self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
TableViewCell自适应
rowHeight
与estimatedRowHeight
缺一不可,同时要保证约束闭环,才能达到自适应的效果。Section Header与Section Footer同理。
- 代码设置
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;//不能兼容iOS9
self.tableView.estimatedRowHeight = 44;//兼容iOS9
-
Xib设置(均能兼容)
常见系统控件高度表
控件 | 竖屏 | 横屏 |
---|---|---|
StatusBar | 20 | 0 |
StatusBar(X) | 44 | 0 |
NavigationBar | 44 | 32 |
NavigationBar(XR、Max) | 44 | 44 |
TabBar | 49 | 32 |
TabBar(X) | 83 | 53 |
TabBar(XR、Max) | 83 | 70 |
HomeIndicator | 34 | 21 |
常用宏定义
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds
#define SCREEN_HEIGHT SCREEN_BOUNDS.size.height
#define SCREEN_WIDTH SCREEN_BOUNDS.size.width
#define SCREEN_3_5_INCH_WIDTH 320.0f
#define SCREEN_3_5_INCH_HEIGHT 480.0f
#define SCREEN_4_INCH_WIDTH 320.0f
#define SCREEN_4_INCH_HEIGHT 568.0f
#define SCREEN_4_7_INCH_WIDTH 375.0f
#define SCREEN_4_7_INCH_HEIGHT 667.0f
#define SCREEN_5_5_INCH_WIDTH 414.0f
#define SCREEN_5_5_INCH_HEIGHT 736.0f
#define SCREEN_5_8_INCH_WIDTH 375.0f
#define SCREEN_5_8_INCH_HEIGHT 812.0f
#define SCREEN_6_5_INCH_WIDTH 414.0f // 6.1寸的XR宽高和6.5的XSMax 两者的pt相同,但是px不同
#define SCREEN_6_5_INCH_HEIGHT 896.0f
#define IS_SCREEN_3_5_INCH (([UIScreen mainScreen].bounds.size.height == SCREEN_3_5_INCH_HEIGHT && [UIScreen mainScreen].bounds.size.width == SCREEN_3_5_INCH_WIDTH)||([UIScreen mainScreen].bounds.size.height == SCREEN_3_5_INCH_WIDTH && [UIScreen mainScreen].bounds.size.width == SCREEN_3_5_INCH_HEIGHT))
#define IS_SCREEN_4_INCH (([UIScreen mainScreen].bounds.size.height == SCREEN_4_INCH_HEIGHT && [UIScreen mainScreen].bounds.size.width == SCREEN_4_INCH_WIDTH)||([UIScreen mainScreen].bounds.size.height == SCREEN_4_INCH_WIDTH && [UIScreen mainScreen].bounds.size.width == SCREEN_4_INCH_HEIGHT))
#define IS_SCREEN_4_7_INCH (([UIScreen mainScreen].bounds.size.height == SCREEN_4_7_INCH_HEIGHT && [UIScreen mainScreen].bounds.size.width == SCREEN_4_7_INCH_WIDTH)||([UIScreen mainScreen].bounds.size.height == SCREEN_4_7_INCH_WIDTH && [UIScreen mainScreen].bounds.size.width == SCREEN_4_7_INCH_HEIGHT))
#define IS_SCREEN_5_5_INCH (([UIScreen mainScreen].bounds.size.height == SCREEN_5_5_INCH_HEIGHT && [UIScreen mainScreen].bounds.size.width == SCREEN_5_5_INCH_WIDTH)||([UIScreen mainScreen].bounds.size.height == SCREEN_5_5_INCH_WIDTH && [UIScreen mainScreen].bounds.size.width == SCREEN_5_5_INCH_HEIGHT))
#define IS_SCREEN_5_8_INCH (([UIScreen mainScreen].bounds.size.height == SCREEN_5_8_INCH_HEIGHT && [UIScreen mainScreen].bounds.size.width == SCREEN_5_8_INCH_WIDTH)||([UIScreen mainScreen].bounds.size.height == SCREEN_5_8_INCH_WIDTH && [UIScreen mainScreen].bounds.size.width == SCREEN_5_8_INCH_HEIGHT))
#define IS_SCREEN_6_5_INCH (([UIScreen mainScreen].bounds.size.height == SCREEN_6_5_INCH_HEIGHT && [UIScreen mainScreen].bounds.size.width == SCREEN_6_5_INCH_WIDTH)||([UIScreen mainScreen].bounds.size.height == SCREEN_6_5_INCH_WIDTH && [UIScreen mainScreen].bounds.size.width == SCREEN_6_5_INCH_HEIGHT))
#define IS_IPHONEX (IS_SCREEN_6_5_INCH || IS_SCREEN_5_8_INCH)
#define TabBarHeight(tabBar) CGRectGetHeight(tabBar.frame)
#define NavigationBarHeight(navigationBar) CGRectGetHeight(navigationBar.frame)
#define StatusBarHeight CGRectGetHeight([UIApplication sharedApplication].statusBarFrame)
#define HomeIndicatorV (IS_IPHONEX ? 34 : 0)//竖屏
#define HomeIndicatorH (IS_IPHONEX ? 21 : 0)//横屏
相关链接
iPhone X + iOS 11 适配指南
如何适配iPhone X?来看QQ 音乐这个实战案例复盘总结
XCode9的新变化