源码

首页 » 归档 » 源码 » iOS11 适配之导航栏、tableView、searchBar遇到的bug-ios学习从入门到精通尽在姬长信

iOS11 适配之导航栏、tableView、searchBar遇到的bug-ios学习从入门到精通尽在姬长信

分享最热门资讯

最近刚接手了个项目,在iOS11之前都没有问题,但是在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;

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

这几个bug着实让我浪费了好多时间,希望我写下这片文章对你们有多帮助

ios学习入门精通尽在长信

(0)

本文由 姬長信 创作,文章地址:https://blog.isoyu.com/archives/20537.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 29, 2017 at 08:20 下午

关键词:, , , ,

热评文章

发表回复

[必填]

我是人?

提交后请等待三秒以免造成未提交成功和重复