详解iOS11关于导航栏问题

2025-05-29 0 59

前言

ios11导航栏除了新加入了largetitles和searchcontroller两个新特性,可能是加入largetitles的原因其结构较ios 10发生了些变化。

ios11之前导航栏的navigationbarbutton则直接添加在navigationbar上面

详解iOS11关于导航栏问题

在ios11之后,苹果添加了新的类来管理,可以看到titleview直接加在_uinavigationbarcontentview上,uibarbuttonitem则添加在_uibuttonbarstackview上面,而_uibuttonbarstackview则添加在_uinavigationbarcontentview上面,最后添加到uinavigationbar上面,如下图所示:

详解iOS11关于导航栏问题

由于结构的变化,在ios 11中我们自定义设置leftbarbuttonitem,其点击区域变得很小,让人点的很焦灼,如下图绿色区域所示:

详解iOS11关于导航栏问题

具体代码如下,设置的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。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 详解iOS11关于导航栏问题 https://www.kuaiidc.com/89847.html

相关文章

发表评论
暂无评论