87、代码适配IphoneX

一.APP在iphoneX运行后不能占满,上下都有多余的边

解决方法:把旧的image.xcassets中的LaunchImage删掉,重新创建并在Images.xcassets中为iPhone X添加一个LaunchImage,新的启动图尺寸为1125px × 2436px(375pt × 812pt @3x).

问题原因:1.应用启动后的显示尺寸会根据启动图的大小来显示,因为旧的工程没有iPhoneX的尺寸,所以就会出现上下有多余边的问题. 
2.旧工程工程的xcassets没有iPhoneX,所以需要把旧的删掉,重新创建,才可以添加iPhoneX尺寸的启动

二、局部适配

 因为iphoneX的屏幕上下边角是有弧度的,如果没有适配弧度是看不到的

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

ios11以后,那两个参照已经deprecated(过时)了,而被Safa Area 取代。

Safa Area 要求最低支持IOS9.0  最终总结SafaArea

三、titleview适配

很多APP都会在navigation的titleview上添加搜索框,在iOS11上会出现这种情况的偏移

解决方法:在替换titleview的view里实现方法

-(CGSize)intrinsicContentSize{

return UILayoutFittingExpandedSize;

}

四、适配下拉刷新

下拉在正常状态下会有尺寸偏移

解决方法:在controller中添加


if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
解决方法二:

1.self.automaticallyAdjustsScrollViewInsets = NO;

2. self.extendedLayoutIncludesOpaqueBars = YES;

3.self.edgesForExtendedLayout = UIRectEdgeTop;

问题很多:多数情况是在隐藏导航栏出现的,因为iOS11后tableview的automaticallyAdjustScrollViewInsets属性被废弃了,顶部久多了一定的inset,也可能是SafeArea(安全区域)的原因,关于SafaArea(安全区域)适配下边会写

如果还是有尺寸的偏移那就是tableView的另一个解决办法

五、tableview间隙问题

解决方法:实现-tableView: viewForFooterInSection: 和 -tableView: viewForHeaderInSection:方法.或者设置tableview的estimatedRowHeight estimatedSectionHeaderHeight estimatedSectionFooterHeight三个属性为0

问题原因:iOS 11中如果不实现-tableView: viewForFooterInSection: 和 -tableView: viewForHeaderInSection:,那么-tableView: heightForHeaderInSection:和- tableView: heightForFooterInSection:不会被调用。 
这是因为在tableview中的 estimatedRowHeight estimatedSectionHeaderHeight estimatedSectionFooterHeight三个高度估算属性由默认的0变成了UITableViewAutomaticDimension,导致高度计算不对。

六、SafeArea适配

1.官方的安全区域的指导图,旧的项目运行在iphoneX上最大显著的问题就是导航和底部tabbar的位置偏移,因为之前的导航高度为64,随意程序基本上都是写死值,适配的话就是各种苦逼了,xib和代码的各有各的库,总之所有的地方都要通过判断是否是iphoneX来适配高度

为了适配我们添加了全局的宏

//是否是iPhone X
#define is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
//状态栏高度
#define StatusBarHeight (is_iPhoneX ? 44.f : 20.f)
// 导航高度
#define NavigationBarHeight 44.f
// Tabbar高度. 49 + 34 = 83
#define TabbarHeight (is_iPhoneX ? 83.f : 49.f)
// Tabbar安全区域底部间隙
#define TabbarSafeBottomMargin (is_iPhoneX ? 34.f : 0.f)
// 状态栏和导航高度
#define StatusBarAndNavigationBarHeight (is_iPhoneX ? 88.f : 64.f)

#define ViewSafeAreInsets(view) ({UIEdgeInsets insets; if(@available(iOS 11.0, *)) {insets = view.safeAreaInsets;} else {insets = UIEdgeInsetsZero;} insets;})

2.在Storyboard中有SafeArea的选项,但是只支持ios9以上,所以要进行修改

3.如果使用了Masonry进行布局,就要适配safeArea

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

   make.edges.equalTo(self.view.safeAreaInsets);

} else{

   make.edges.equalto(self.view);

}

4.在没有适配的页面底部是到屏幕底的,需要设置安全区域内,把底部的黑条留出来

七、UIImagePickerController设置导航背景图

[self.navigationBar setBackgroundImage:[UIImage imagedNamed:@""] forBarMetrics:UIBarMetricsDefault];

八.Home Indicator

Home indicator 的设置类似于 prefersStatusBarStyle,iOS 11 增加了 UIViewController 的一个 UIHomeIndicatorAutoHidden 分类来控制 home 键的自动隐藏。通常在全屏播放视频,全屏游戏等场景下会需要用到此特性。


@interface UIViewController (UIHomeIndicatorAutoHidden)

// Override to return a child view controller or nil. If non-nil, that view controller's home indicator auto-hiding will be used. If nil, self is used. Whenever the return value changes, -setNeedsHomeIndicatorAutoHiddenUpdate should be called.
- (nullable UIViewController *)childViewControllerForHomeIndicatorAutoHidden API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos);
// Controls the application's preferred home indicator auto-hiding when this view controller is shown.
- (BOOL)prefersHomeIndicatorAutoHidden API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos);

// This should be called whenever the return values for the view controller's home indicator auto-hiding have changed.
- (void)setNeedsUpdateOfHomeIndicatorAutoHidden API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos);
@end

猜你喜欢

转载自www.cnblogs.com/qiangzheVSruozhe/p/10683053.html
今日推荐