美文网首页
iOS 11 相关适配

iOS 11 相关适配

作者: Janyau | 来源:发表于2017-11-27 11:28 被阅读66次

说明: 目前调整的项目是基于纯代码构建.

主线程检查

Main Thread Checker: UI API called on a background thread: -[UIApplication setNetworkActivityIndicatorVisible:]
主线程检查: 可配置, 若配置了, 则 Xcode 会自动检查哪些代码需要在主线程执行. 此处去掉主线程检查, 因为 AFNetworking 无法编译通过, 升级到最新版也不行.

- (void)updateNetworkActivityIndicatorVisibility {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:[self isNetworkActivityIndicatorVisible]];
} // 这里检查出要在主线程执行

解决:


Snip20171120_19.png

明确指定函数类型

Xcode 9 报错, Call to 'abs' is ambiguous , 查看 abs 函数发现有多个 abs() , 系统不知道用的是哪一个 abs() , 故要指定唯一的函数.

Snip20171204_120.png

解决:
将 abs() 改为 fabs().

TableView内容下移

iOS 11 中 UIViewController 的 automaticallyAdjustsScrollViewInsets 属性被废弃. 所有基类里的关于 automaticallyAdjustsScrollViewInsets 的设置无效了, 系统默认的偏移会生效, 故其内容会下移.

Snip20171121_23.png Snip20171204_124.png

解决:
因为项目里只用到 TableView , 故只设置 TableView.

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self addTableView];

#ifdef __IPHONE_11_0
    if (@available(iOS 11.0, *)){
        if ([self getCurTableView].contentInsetAdjustmentBehavior != UIScrollViewContentInsetAdjustmentNever) {
            [[self getCurTableView] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        }
    }
#endif
}

ToolBar 点击事件无效

查看 ToolBar 的层级结构发现添加到 ToolBar 的 view 处于中间位置, ToolBar 自带的 subView 处于最外层, 拦截了点击事件.


Snip20171121_30.png

解决:
替换成普通的 UIView.

tableView 的 section 有留白

iOS 11上发生 tableView 顶部有留白,原因是代码中只实现了heightForHeaderInSection方法,而没有实现viewForHeaderInSection方法.
在iOS 11中默认启用 Self-Sizing 未使用 AutoLayout 的 TableView 中的高度会出现问题 . Self-Sizing 在iOS11下是默认开启的,Headers, footers, and cells 都默认开启Self-Sizing ,所有estimated 高度默认值从iOS11之前的 0 改变为 UITableViewAutomaticDimension.

解决:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
UISearchBar 在 iOS 11 中字体变大, 背景间隔太小
Snip20171204_126.png

解决:
在创建 SearchBar 时加入如下代码.

#ifdef __IPHONE_11_0
        if (@available(iOS 11.0, *)){
            [searchBar setSearchFieldBackgroundImage:[self searchFieldBackgroundImage] forState:UIControlStateNormal];
            UIView *searchView = searchBar.subviews.firstObject;
            if (searchView && [searchView isKindOfClass:[UIView class]]) {
                NSArray *searchViewList = searchView.subviews;
                if (searchViewList && [searchViewList isKindOfClass:[NSArray class]]) {
                    for (UIView *subView in searchViewList) {
                        if ([subView isKindOfClass:[UITextField class]]) {
                            UITextField *textF = (UITextField *)subView;
                            [textF setDefaultTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:CellFontSize]}];
                            break;
                        }
                    }
                }
            }
        }
#endif

- (UIImage *)searchFieldBackgroundImage
{
    UIColor*color = [UIColor whiteColor];
    CGFloat cornerRadius = 5;
    CGRect rect =CGRectMake(0,0,28,28);
    
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    roundedRect.lineWidth = 0;
    
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
    [color setFill];
    [roundedRect fill];
    [roundedRect stroke];
    [roundedRect addClip];
    
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

上拉刷新

Snip20171207_23.png

解决:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (IS_IOS_LATER(11.0)) {
        if (self.downRefresh.top != [self getCurTableView].bottom) {
            self.downRefresh.top = [self getCurTableView].bottom;
        }
    }
}

上拉刷新跟着滑动

相关文章

网友评论

      本文标题:iOS 11 相关适配

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