美文网首页selector
iOS开发之iOS11适配问题

iOS开发之iOS11适配问题

作者: a6750108cb56 | 来源:发表于2017-12-07 11:56 被阅读207次

`## 1.tableView的适配

  1. 导航栏向上跑了部分距离:宏定义一个高度
    参见第6条
  2. VC中的tableView向下移动部分距离,以及cell直接的间隔会无故拉大:在你的tableView下面添加这句话
if (@available(iOS 11.0, *)) {

UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

} else {

// Fallback on earlier versions

}
  1. 如果你的cell 之间的间距拉大,就在self.xf_tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);这个约束后面添加下面三个约束
self.xf_tableView.estimatedRowHeight = 0;

self.xf_tableView.estimatedSectionHeaderHeight = 0;

self.xf_tableView.estimatedSectionFooterHeight = 0;
  1. 加载webView的时候会向下移动部分距离:给你的web添加下面约束
if (@available(iOS 11.0, *)) {

webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

} else {

// Fallback on earlier versions

}
  1. 创建搜索框
UIView *titleView = [[TUIView alloc] init];

titleView.py_x = PYSEARCH_MARGIN * 0.5;

titleView.py_y = 7;

titleView.py_width = self.view.py_width - 64 - titleView.py_x * 2;

titleView.py_height = 30;

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:titleView.bounds];

[titleView addSubview:searchBar];

self.navigationItem.titleView = titleView;  
  1. 升级Xcode 9 + iOS 11后,发现原本没问题的collectionView和tableView像是中了风一样,头部刷新UI出现了错乱。
    查阅发现 iOS11弃用了automaticallyAdjustsScrollViewInsets属性,新增contentInsetAdjustmentBehavior来替代它
    关于 contentInsetAdjustmentBehavior
@available(iOS 11.0, *)
public enum UIScrollViewContentInsetAdjustmentBehavior : Int {

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

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

    case never // contentInset is not adjusted

    case always // contentInset is always adjusted by the scroll view's safeAreaInsets
}

UIScrollViewContentInsetAdjustmentBehavior 是一个枚举类型,值有以下几种:

* -automatic 和scrollableAxes一样,scrollView会自动计算和适应顶部和底部的内边距并且在scrollView 不可滚动时,也会设置内边距.
* -scrollableAxes 自动计算内边距.
* -never不计算内边距
* -always 根据safeAreaInsets 计算内边距

很显然,我们这里要设置为 never

        //声明tableView的位置 添加下面代码
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
            _tableView.scrollIndicatorInsets = _tableView.contentInset;
        }

2.iOS11之后,跳转App Store

 NSString *appstoreUrlString = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@",AppStoreAppId ];
NSURL * url = [NSURL URLWithString:appstoreUrlString];
if ([[UIApplication sharedApplication] canOpenURL:url]){
        [[UIApplication sharedApplication]openURL:url];
 }else{
        WKLog(@"can not open");
}

需要改成

NSString *appstoreUrlString = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/idXXXXXX?mt=8&action=write-review", AppStoreAppId ];
NSURL * url = [NSURL URLWithString:appstoreUrlString];
if ([[UIApplication sharedApplication] canOpenURL:url]){
                    [[UIApplication sharedApplication]openURL:url];
 }else{
        WKLog(@"can not open");
}

3.iPhoneX的适配

需要一张 1125 *2436 的图片加入启动页LaunchImage。

4.可以使用NFCiOS11新特性 - Core NFC

5.asset新特性

  • 另外,在Xcode9中提供“基于矢量的 asset”的支持。、
    在之前的Xcode中,添加 image asset 的时候,我们可以添加pdf格式的。但Xcode只是把@1x,@2x,@3x资源提供给app包,而不是矢量图。
    在Xcode9中,提供了一个新的选项叫做"Preserve Vector Data"
  • 可以添加Color Asset

6.根据Navigation、Tabbar的高度宏定义

#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define Is_Iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define Is_Iphone_X (Is_Iphone && ScreenHeight == 812.0)
#define NaviHeight Is_Iphone_X ? 88 : 64
#define TabbarHeight Is_Iphone_X ? 83 : 49
#define BottomHeight Is_Iphone_X ? 34 : 0

7.UIKit框架

  • 本次iOS11系统更新UIKit较多,主要是因为iOS11新增的文件管理功能并没有提供单独的框架,而是被直接继承在UIKit框架中,测试版问题比较多,实际还是以正式版为准

  • 问题修复

    • 设置UITableView的delaysContentTouches延迟响应属性为NO,再也不会立即触发cell的响应事件(delaysContentTouches是UIScrollerView的属性,叫做延迟响应处理。默认为YES延迟处理,作用就是当点击scrollerview中的按钮时不会被系统判定为ScrollerView的滚动手势,对这个属性不了解的小白们可以百度哈,这里笔者不再详细累述)
      • Setting the delaysContentTouches property of UITableView to false now works correctly and results in cells highlighting immediately on a touch down event. (27102830)
  • 新的变化

    • UIDocumentBrowserViewController(显示沙盒文件浏览器)不会显示UIDocumentPickerExtensionViewController列表中的应用程序扩展来源。虽然这些会在将在的测试版中重新启用,请考虑使用一个NSFileProviderExtension相反,因为它们是集成在UIDocumentBrowserViewController而不是在一个单独的表

      • UIDocumentBrowserViewController does not show UIDocumentPickerExtensionViewController app extensions in the list of sources. While these will be re-enabled in a future beta, please consider using an NSFileProviderExtension instead, as they are integrated in UIDocumentBrowserViewController instead of being presented in a separate sheet. (31975976)
    • 当拖动文件超过5项,文件移动/取消动画使用一个默认的动画。前五个文件夹系统调用移动/取消预览视图,即使它们没有使用。在后面的测试,系统不会让这些调用,而不是为动画提供了一个额外的API与项目的自定义动画不能应用。
      *When dragging more than 5 items, the drop/cancel animation uses a default animation. The system calls the dropping/cancelling preview calls for the first 5 of these items, even though they are not used. In a later beta, the system will not make these calls, and instead provides an additional API for animating alongside items for which a custom animation cannot be applied. (32299785)

    • 使用UITargetedDragPreview和UIDragPreview而不是UIURLDragPreviewView,因为在下一个版本中这个API将会被移除(移除并不意味着消失,有可能在测试版中开发,正式版中成了私有API)

      • Do not use the class UIURLDragPreviewView as it will be removed in a later beta.To make drag previews of URLs, use UIDragPreview and UITargetedDragPreview. (32306613)
    • 在storyboard或者xib中,Xcode9之后的tableview显示cell和header和footer将会更加的区分明显(实际上就是storyboard的显示细节小优化)

      • Table views in Interface Builder documents compiled with Xcode 9.0 may have fixed heights for rows, section headers, and section footers, and may not self-size by default. (32188814)
    • 在iOS11中UITableView有了一个新的系统手势可以快速选择行:两根手指快速的轻击cell,可以同时选中两个cell进入编辑状态。如果两个手指存在不同步问题,则会默认识别其中的一个手指表示单选cell

      • UITableView has a new system gesture for quickly selecting rows. A two-finger tap on table views that support multiple selection sets the table view to editing, if it is not already, and select the tapped row. Unlike a single-finger tap, which toggles selection, the two- finger tap always selects the tapped row and has no effect if the row is already selected. This new gesture is enabled only in apps built for iOS 11. (29127664)
    • UITableView新增了一个属性separatorInsetReference,作用是可以自定义一个cell分割线的边距

      • UITableView has a new property, separatorInsetReference, that changes how a custom value set in the separatorInset property is interpreted. By default, the value is .fromCellEdges, which means that custom separatorInset values are always interpreted as an inset from the edges of the cell, even in cases where table view would normally use larger automatic insets for the separator, such as a very wide table view when the cellLayoutMarginsFollowReadableWidth property is true
      • Set the separatorInsetReference to .fromAutomaticInsets to use the separatorInset as a fixed distance from the automatic inset that a table view would normally use
      • Note that you can always use UITableViewAutomaticDimension for any edge of the separatorInset that you set, to receive the automatic inset on that edge. (31000402)
    • iOS11中,UITableView的cell或者表头表尾默认采用自适应高度的方案,当然如果之前的tableview不想使用这个功能可以直接禁掉,或者在自适应高度代理中返回0即可

      • Table views now use estimated heights by default, which also means that cells and section header/footer views now self-size by default. The default value of the estimatedRowHeight, estimatedSectionHeaderHeight, and estimatedSectionFooterHeight properties is now UITableViewAutomaticDimension, which means the table view selects an estimated height to use. You should still provide a more accurate estimate for each property if possible, which is your best guess of the average value of the actual heights. If you have existing table view code that behaves differently when you build your app with the iOS 11 SDK, and you don’t want to adopt self-sizing, you can restore the previous behavior by disabling estimated heights by setting a value of zero for each estimated height property. (30197915)
    • 当UITableViewCell的内容(主要是文本)高度过高时,可以通过设置UITableViewCell的numberOfLines属性来实现类似于UILabel一样的高度自适应变化

      • When the user selects one of the Accessibility Larger Text content sizes as their preferred content size, UITableViewCell uses a new default layout that is optimized for the larger sizes. The numberOfLines property of UITableViewCell labels (both textLabel and detailTextLabel) changes to 0 (unlimited) by default when the Accessibility content sizes are in use. You may override this by explicitly setting the numberOfLines property. The standard image view and accessory views are aligned with the first line of text in the cell, and cell text wraps around the image in order to maximize the amount of text displayed per line. For the Value1, Value2, and Subtitle styles, the detailTextLabel is arranged underneath the standard textLabel. For UISwitch accessory views, the switch is located below the labels and aligned with the leading edge of the text. Because of these changes, which increase the height of the cell content at Accessibility content sizes, it’s important to use self-sizing cells to ensure the row height increases as necessary, and to avoid using a fixed row height for all content sizes. (28102750)

8.开发建议

  • self.automaticallyAdjustsScrollViewInsets = NO。
  • 因为要适配iPhone X动态改变导航和tabbar的高度,所以最外层不建议用xib,直接通过代码适配最好。

参考资料:

相关文章

网友评论

    本文标题: iOS开发之iOS11适配问题

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