iOS 11 UINavigationItem 去除左右间隙的方法
2019/7/9 22:50:09
本文主要是介绍iOS 11 UINavigationItem 去除左右间隙的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前言
iOS 11版本由于对于Nav层级结构的改变,导致以前的方法无法达到理想的移动效果,使顶部的按钮完全靠左,或者是靠右.
修改思路
在iOS11之前保持原有方式进行设置,iOS11之后进行额外的边距约束修改达到移动效果.
从viewDebug的界面上观察可以看到需要将UIButtonBarStackView距离左边和右边的16的约束改为0即可.
核心代码
配置导航器view代码
//0:leftBarButtonItems,1:rightBarButtonItems - (void)initBarItem:(UIView*)view withType:(int)type{ UIBarButtonItem * buttonItem = [[UIBarButtonItem alloc]initWithCustomView:view]; //解决按钮不靠左 靠右的问题.iOS 11系统需要单独处理 UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; spaceItem.width = -16;//这个值可以根据自己需要自己调整 switch (type) { case 0: if (!IS_IOS_VERSION_11) { self.navigationItem.leftBarButtonItems =@[spaceItem,buttonItem]; }else{ self.navigationItem.leftBarButtonItems =@[buttonItem]; } break; case 1: if (!IS_IOS_VERSION_11) { self.navigationItem.rightBarButtonItems =@[spaceItem,buttonItem]; }else{ self.navigationItem.rightBarButtonItems =@[buttonItem]; } break; default: break; } }
处理iOS11情况下的偏移问题,将边距为16的约束的值改为0.
-(void)viewDidLayoutSubviews{ if (!IS_IOS_VERSION_11) return; UINavigationItem * item=self.navigationItem; NSArray * array=item.leftBarButtonItems; if (array&&array.count!=0){ //这里需要注意,你设置的第一个leftBarButtonItem的customeView不能是空的,也就是不要设置UIBarButtonSystemItemFixedSpace这种风格的item UIBarButtonItem * buttonItem=array[0]; UIView * view =[[[buttonItem.customView superview] superview] superview]; NSArray * arrayConstraint=view.constraints; for (NSLayoutConstraint * constant in arrayConstraint) { if (fabs(constant.constant)==16) { constant.constant=0; } } } }
改后效果.png
Demo地址:https://github.com/StoneMover/navDemo.git
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于iOS 11 UINavigationItem 去除左右间隙的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创
- 2023-11-10【iOS开发】iOS App的加固保护原理:使用ipaguard混淆加固
- 2023-09-30最强大的iOS应用源码保护工具:Ipa Guard,保护你的商业机密代码
- 2023-09-07iOS安全加固探讨:代码混淆、类名方法名混淆等方法
- 2023-09-05iOS代码加固与保护方法详解 - 提升iOS应用安全性的关键步骤