IOS11界面适配问题

来源:互联网 发布:ubuntu 安装skype 编辑:程序博客网 时间:2024/05/20 05:25

兴冲冲升级Xcode 9+iOS 11后,发现我的项目变得乱七八糟,主要体现在tableview、导航栏这两个方面;

赶紧逐个查看,发现了以下四个问题:

1.tableview大部分都下移了大概30个像素

2.导航栏的返回按钮下移

3.键盘失去了完成按钮

4.会出现紫色警告,基本上都是如:xxxx must be called from main thread only


如果有我相同问题的,可以往下看



1.tableview大部分都下移了大概30个像素

仔细观察发现带headView的和UITableViewStylePlain类型的不受影响

而Group类型的我都设置了代理方法heightForHeaderInSection,打断点发现这个代理方法没有执行
搜了一堆东西,发现是IOS11默认开启Self-Sizing如果不实现-tableView: viewForHeaderInSection:和-tableView: viewForFooterInSection: ,则heightForHeaderInSection和heightForFooterInSection都不会被调用,导致它们都变成了默认高度,tableView的estimatedRowHeight、estimatedSectionHeaderHeight、 estimatedSectionFooterHeight这三个高度估算属性由默认的0变成了UITableViewAutomaticDimension将tableview的预估高度的属性全部设置为0即可,代码如下
tableView.estimatedRowHeight = 0; 
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;

我是写了一个tableview的基类,重写

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    

    self = [superinitWithFrame:framestyle:style];

    if (self) {

        self.estimatedRowHeight =0;

        self.estimatedSectionHeaderHeight =0;

        self.estimatedSectionFooterHeight =0;

    }

    returnself;

}

然后搜索所有用了这个方法的tableview,将他们的类型设置为这个基类,解决


2.导航栏的返回按钮下移

这里是因为我之前用的下面这个方法来隐藏返回按钮的文字

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];

现在我改成在控制器基类中添加并执行以下方法

//将BackBarButtonItem修改为@""

- (void)hideBackBarButtonItem{

    /**作用:居中NavBarTitle 原理:修改backbarbuttonitem的title,让其字符串占位少**/

    NSArray *viewControllerArray = [self.navigationControllerviewControllers];

    long previousViewControllerIndex = [viewControllerArrayindexOfObject:self] -1;

    UIViewController *previousVC;

    if (previousViewControllerIndex >=0) {

        previousVC = [viewControllerArray objectAtIndex:previousViewControllerIndex];

        previousVC.navigationItem.backBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@""style:UIBarButtonItemStylePlaintarget:selfaction:nil];

    }

}

解决


3.键盘失去了完成按钮

我的键盘的弹出和落下用了第三方库IQKeyboard,这里更到最新版本即可


4.会出现紫色警告

所有的UI操作都要在主线程中执行,这里我没写让它在主线程执行,所以会出这个警告,如果觉得烦,这个警告可以被屏蔽掉(不推荐屏蔽)

点击Product--->Scheme--->Edit Scheme,把这个Main Thread Checker的勾去掉就可以了