iOS11 适配之导航栏、tableView、searchBar遇到的bug

iOS11上出现了个别屏幕适配问题,其中包括:1、push进入下一个VC之后,导航栏在会往上移部分距离,大概20像素;2、VC中的tableView向下移动部分距离,以及cell直接的间隔会无故拉大;3、加载webView的时候会向下移动部分距离;4、放在导航栏上面的searchBar消失不见。虽然网上很多文章介绍解决的方法,但是我还是查阅了大部分简书,博客,也花费了差不多两天时间才把这个bug解决完。下面是出现bug界面的图片,希望对你们能有所帮助。

6882374-d2a23f4bc8f63842.jpg

6882374-069aae4285be891f.jpg

6882374-f40acf5ce13aef62.jpg

其实解决这些bug很简单,只不过不同的人遇到的问题不同罢了,至于为什么会出现这些bug,你们可以去官方或者大神的简书去看看iOS界面布局的一些改变。现在就针对我们项目当中出现的问题,我一一给出答案,有不懂的,可以私信我。

1、导航栏向上跑了部分距离:宏定义一个高度

#define NAVIGATION_HEIGHT (CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]) + CGRectGetHeight(self.navigationController.navigationBar.frame))

在你设置的self.navigationBar.frame = CGRectMake(0, 0,ScreenWidth, NAVIGATION_HEIGHT);下面添加

#ifdef __IPHONE_11_0

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

self.navigationBar.frame = CGRectMake(0, STATUSBAR_HEIGHT,ScreenWidth, NAVIGATION_HEIGHT);

}

#endif

2、VC中的tableView向下移动部分距离,以及cell直接的间隔会无故拉大:

//在你的tableView下面添加这句话

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

UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

} else {

// Fallback on earlier versions

}

//如果你的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;

3、加载webView的时候会向下移动部分距离:给你的web添加下面约束

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

webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

} else {

// Fallback on earlier versions

}

4、放在导航栏上面的searchBar消失不见:

之前我的代码是这样写的:

// 创建搜索框

UIView *titleView = [[UIView 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;

这样你会发现搜索框不显示,更改后的代码是将titleView的UIView重写为TUIView

新建一个TUIView类,在该类的.m里面实现以下方法:

#import "TUIView.h"

@implementation TUIView

-(CGSize)intrinsicContentSize

{

return UILayoutFittingExpandedSize;

}

@end

// 创建搜索框

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;

这样搜索框就显示出来了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值