前言
ios11导航栏除了新加入了largetitles和searchcontroller两个新特性,可能是加入largetitles的原因其结构较ios 10发生了些变化。
ios11之前导航栏的navigationbarbutton则直接添加在navigationbar上面
在ios11之后,苹果添加了新的类来管理,可以看到titleview直接加在_uinavigationbarcontentview上,uibarbuttonitem则添加在_uibuttonbarstackview上面,而_uibuttonbarstackview则添加在_uinavigationbarcontentview上面,最后添加到uinavigationbar上面,如下图所示:
由于结构的变化,在ios 11中我们自定义设置leftbarbuttonitem,其点击区域变得很小,让人点的很焦灼,如下图绿色区域所示:
具体代码如下,设置的frame在这里并没有什么卵用,点击区域依然只有图片原本的size那么大:
1
2
3
4
5
6
7
|
uibutton *btn = [[uibutton alloc] initwithframe:cgrectmake(0, 0, 60, 40)];
[btn setimage:imagewhite forstate:uicontrolstatenormal];
[btn addtarget:self action:@selector(bpback) forcontrolevents:uicontroleventtouchupinside];
btn.backgroundcolor = [uicolor greencolor];
uibarbuttonitem *leftitem = [[uibarbuttonitem alloc] initwithcustomview:btn];
leftitem.width = 60;
self.navigationitem.leftbarbuttonitem = leftitem;
|
为了能增加点击区域,我们就需要增加button的size,然后就想到通过改变contentedgeinsets来增大button的size,
1
2
3
4
5
6
7
8
9
10
|
...
...
btn.backgroundcolor = [uicolor greencolor];
if (@available(ios 11.0,*)) {
[btn setcontentmode:uiviewcontentmodescaletofill];
[btn setcontentedgeinsets:uiedgeinsetsmake(0, 5, 5, 20)];
}
uibarbuttonitem *leftitem = [[uibarbuttonitem alloc] initwithcustomview:btn];
...
...
|
另:searchbar设置为titleview,会导致navigation的高度发生异常(ps:push到下一个界面,下个界面的view距离navigation出现了一段黑色区域)需要处理下:
1
2
3
4
5
6
7
8
9
10
11
|
cgrect frame = cgrectmake(0, 0, 150, 44);
uisearchbar *search = [[uisearchbar alloc] initwithframe:frame];
search.placeholder = @ "搜索" ;
search.delegate = self;
uitextfield *searchfield=[search valueforkey:@ "_searchfield" ];
searchfield.backgroundcolor = [uicolor grouptableviewbackgroundcolor];
// --- ios 11异常处理
if (@available(ios 11.0, *)) {
[[search.heightanchor constraintequaltoconstant:44] setactive:yes];
}
self.navigationitem.titleview = search;
|
详细资料参考:
https://stackoverflow.com/questions/45997996/ios-11-uisearchbar-in-uinavigationbar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 11
-
2025-06-04 47
-
IntelliJ IDEA创建maven多模块项目(图文教程)
2025-05-29 102 -
2025-05-25 55
-
2025-05-29 37