相信很多同学都知道在iOS7之后调整导航栏两侧按钮距离左右间距,其实就是在左右barButtonItem的数组中添加一个宽度为负的占位item。

- (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem
{
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = -8;
[self setLeftBarButtonItems:@[space, leftBarButtonItem]];
}

但是在iOS11之后,发现该方法失效了。新的思路和iOS7的完全不一样,我们给UINavigationBarContentView加一条约束。怎么加呢?

1 自定义一个customView,使用initWithCustomView创建UIBarButtonItem。

2 在customView的layoutSubviews方法中找到UINavigationBarContentView,添加customView和UINavigationBarContentView之间的约束。

customView定义如下:

#import "UIView.h"

typedef NS_ENUM(NSInteger, LFBarButtonItemViewType) {
LFBarButtonItemViewTypeLeft,
LFBarButtonItemViewTypeRight,
}; @interface LFBarButtonItemView : UIView @property (nonatomic, assign) LFBarButtonItemViewType type; @end
@implementation LFBarButtonItemView

- (void)layoutSubviews {
[super layoutSubviews];
if (iOSVersion < 11.0) {
return;
}
//Here is a workaround on iOS 11 UINavigationBarItem init with custom view, position issue
UIView *view = self;
while (![view isKindOfClass:[UINavigationBar class]] && [view superview] != nil)
{
view = [view superview];
if ([view isKindOfClass:[UIStackView class]] && [view superview] != nil)
{
if (self.type == LFBarButtonItemViewTypeLeft) {
CGFloat margin = 0.0f;
// 5.5寸plus间距大一点
if ([[TWDeviceManager sharedManager] iPhone55]) {
margin = 4.0f;
}
[view.superview addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeLeading multiplier:1.0 constant:margin]];
break;
} else if (self.type == LFBarButtonItemViewTypeRight) {
CGFloat margin = 0.0f;
// 5.5寸plus间距大一点
if ([[TWDeviceManager sharedManager] iPhone55]) {
margin = -4.0f;
}
[view.superview addConstraint:[NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:margin]];
break;
}
}
}
}

在创建self.navigationItem.rightBarButtonItem的地方:

- (void)initNavigationBar {
// rightItem
UIImage *image = [UIImage imageNamed:@"share-icon-dark"];
LFBarButtonItemView *rightItemCustomView = [[LFBarButtonItemView alloc] initWithFrame:CGRectMake(, , image.size.width, image.size.height)];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickOnShareButton)];
[rightItemCustomView addGestureRecognizer:tap];
rightItemCustomView.type = LFBarButtonItemViewTypeRight;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView sizeToFit];
[rightItemCustomView addSubview:imageView];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightItemCustomView];
self.navigationItem.rightBarButtonItem = rightItem;
}

但是这个方法会有约束冲突问题,所以我们把产生冲突的约束删除。可以用xcode查看视图层次,以方便理解。

- (void)layoutSubviews {
[super layoutSubviews];
if (iOSVersion < 11.0) {
return;
}
//Here is a workaround on iOS 11 UINavigationBarItem init with custom view, position issue
UIView *view = self;
while (![view isKindOfClass:[UINavigationBar class]] && [view superview] != nil)
{
view = [view superview];
if ([view isKindOfClass:[UIStackView class]] && [view superview] != nil)
{
if (self.type == LFBarButtonItemViewTypeLeft) {
CGFloat margin = kAppAdaptHeight();
//删除原来的leading约束
for (NSLayoutConstraint *constraint in view.superview.constraints) {
if ([constraint.firstItem isKindOfClass:[UILayoutGuide class]] &&
constraint.firstAttribute == NSLayoutAttributeTrailing) {
[view.superview removeConstraint:constraint];
}
}
//添加新约束
NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeLeading multiplier:1.0 constant:margin];
leadingConstraint.priority = UILayoutPriorityRequired;
[view.superview addConstraint:leadingConstraint]; break;
} else if (self.type == LFBarButtonItemViewTypeRight) {
CGFloat margin = -kAppAdaptHeight(); //删除原来的leading约束
for (NSLayoutConstraint *constraint in view.superview.constraints) {
if ([constraint.firstItem isKindOfClass:[UILayoutGuide class]] &&
constraint.firstAttribute == NSLayoutAttributeTrailing) {
[view.superview removeConstraint:constraint];
}
} NSLayoutConstraint *trailingConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:margin];
trailingConstraint.priority = UILayoutPriorityRequired;
[view.superview addConstraint:trailingConstraint];
break;
}
}
}
}

现在看起来问题解决了,但是某一个界面在push一个新界面之后再返回回来之后位置就还原了 
解决方案其实很简单,只要将设置leftItem的方法写在viewWillAppear中即可,这样即可保证约束不会被系统重置。但是,这样的方案,是不是觉得不完美!

现在有一个终极解决方案:

UINavigationBarContentView平铺在导航栏中作为iOS11的各个按钮的父视图,该视图的所有的子视图都会有一个layoutMargins被占用,也就是系统调整的占位,我们只要把这个置空就行了.那样的话该视图下的所有的子视图的空间就会变成我们想要的那样,当然为了保险起见,该视图的父视图也就是bar的layoutMargins也置空,这样 整个bar就会跟一个普通视图一样了 左右的占位约束就不存在了

给UINavigationBar写一个分类:

#import "UINavigationBar+iOS11Spacing.h"
#import <objc/runtime.h> #define kSpacerWidth kAppAdaptWidth(15) @implementation UINavigationBar (iOS11Spacing) +(void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self swizzleInstanceMethodWithOriginSel:@selector(layoutSubviews)
swizzledSel:@selector(sx_layoutSubviews)];
});
} -(void)sx_layoutSubviews{
[self sx_layoutSubviews]; if (iOS11_OR_LATER && !kSpacerWidth) {//需要调节
self.layoutMargins = UIEdgeInsetsZero;
CGFloat space = kSpacerWidth;
for (UIView *subview in self.subviews) {
if ([NSStringFromClass(subview.class) containsString:@"ContentView"]) {
subview.layoutMargins = UIEdgeInsetsMake(, space, , space);//可修正iOS11之后的偏移
break;
}
}
}
} + (void)swizzleInstanceMethodWithOriginSel:(SEL)oriSel swizzledSel:(SEL)swiSel {
Method originAddObserverMethod = class_getInstanceMethod(self, oriSel);
Method swizzledAddObserverMethod = class_getInstanceMethod(self, swiSel); [self swizzleMethodWithOriginSel:oriSel oriMethod:originAddObserverMethod swizzledSel:swiSel swizzledMethod:swizzledAddObserverMethod class:self];
} + (void)swizzleMethodWithOriginSel:(SEL)oriSel
oriMethod:(Method)oriMethod
swizzledSel:(SEL)swizzledSel
swizzledMethod:(Method)swizzledMethod
class:(Class)cls {
BOOL didAddMethod = class_addMethod(cls, oriSel, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) {
class_replaceMethod(cls, swizzledSel, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
} else {
method_exchangeImplementations(oriMethod, swizzledMethod);
}
}

这样就有一个好处,在原来代码的基础上,判断iOS11,什么都不做,iOS7-iOS11之间版本使用老方法修改间距。

举一个设置LeftBarButtonItem的例子:

#import "UINavigationItem+iOS7Spacing.h"

#import <objc/runtime.h>

#define xSpacerWidth -8

@implementation UINavigationItem (iOS7Spacing)

- (UIBarButtonItem *)spacer

{

UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

space.width = xSpacerWidth;

return space;

}

- (void)mk_setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem

{

if (iOS11_OR_LATER) {

[self mk_setLeftBarButtonItem:leftBarButtonItem];

} else if (iOS7_OR_LATER) {

if (leftBarButtonItem && (leftBarButtonItem.customView !=nil || leftBarButtonItem.image !=nil)) {

[self mk_setLeftBarButtonItem:nil];

[self mk_setLeftBarButtonItems:@[[self spacer], leftBarButtonItem]];

} else {

if (iOS7_OR_LATER) {

[self mk_setLeftBarButtonItems:nil];

}

[self mk_setLeftBarButtonItem:leftBarButtonItem];

}

} else {

[self mk_setLeftBarButtonItem:leftBarButtonItem];

}

}

- (void)mk_setLeftBarButtonItems:(NSArray *)leftBarButtonItems

{

if (iOS7_OR_LATER && leftBarButtonItems && leftBarButtonItems.count > 0 ) {

NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:leftBarButtonItems.count + 1];

[items addObject:[self spacer]];

[items addObjectsFromArray:leftBarButtonItems];

[self mk_setLeftBarButtonItems:items];

} else {

[self mk_setLeftBarButtonItems:leftBarButtonItems];

}

}

- (void)mk_setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem

{

if (iOS11_OR_LATER) {

[self mk_setRightBarButtonItem:rightBarButtonItem];

} else if (iOS7_OR_LATER) {

if (rightBarButtonItem && (rightBarButtonItem.customView !=nil || rightBarButtonItem.image != nil)) {

[self mk_setRightBarButtonItem:nil];

[self mk_setRightBarButtonItems:@[[self spacer], rightBarButtonItem]];

} else {

if (iOS7_OR_LATER) {

[self mk_setRightBarButtonItems:nil];

}

[self mk_setRightBarButtonItem:rightBarButtonItem];

}

} else {

[self mk_setRightBarButtonItem:rightBarButtonItem];

}

}

- (void)mk_setRightBarButtonItems:(NSArray *)rightBarButtonItems

{

if (iOS7_OR_LATER && rightBarButtonItems && rightBarButtonItems.count > 0) {

NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:rightBarButtonItems.count + 1];

[items addObject:[self spacer]];

[items addObjectsFromArray:rightBarButtonItems];

[self mk_setRightBarButtonItems:items];

} else {

[self mk_setRightBarButtonItems:rightBarButtonItems];

}

}

+ (void)mk_swizzle:(SEL)aSelector

{

SEL bSelector = NSSelectorFromString([NSString stringWithFormat:@"mk_%@", NSStringFromSelector(aSelector)]);

Method m1 = class_getInstanceMethod(self, aSelector);

Method m2 = class_getInstanceMethod(self, bSelector);

method_exchangeImplementations(m1, m2);

}

+ (void)load

{

[self mk_swizzle:@selector(setLeftBarButtonItem:)];

[self mk_swizzle:@selector(setLeftBarButtonItems:)];

[self mk_swizzle:@selector(setRightBarButtonItem:)];

[self mk_swizzle:@selector(setRightBarButtonItems:)];

}

参考文章:iOS11 导航栏按钮位置问题的解决

iOS11UINavigationBar的item左右间距调整的更多相关文章

  1. 如何自定义RecycleView item的间距

    引言 在以前使用ListView和GridView时,设置item之间的间距还是相对比较简单的,因为它们的基本属性里面Android已经定义好了,可以直接设置属性值即可.但Google为了通用性和灵活 ...

  2. css中字间距调整(转)

    css中字间距调整 (2014-01-02 13:51:07) 标签: 汉字 英文 间距 letter-spacing word-spacing 杂谈 分类: 网页制作 汉字间字间距设置:letter ...

  3. WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整

    原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...

  4. swiper2 swiper-slide 之间的间距调整

    1.在css中调整间距(我的这个是一行显示三个) swiper-slide-active 可以对这个类进行操作 这个类是显示在页面上的第一个元素 2.设置js //热门新闻 swiper var ne ...

  5. R 多图间距调整

    在R中多图画到一起的时候,各图间距通常默认的较远. 如下图: 1 par(mfcol=c(2,1)) 2 plot(1:100) 3 plot(1:100) 调整图片间距这时我们要用到par()函数中 ...

  6. UILabel字体间距调整

    思路: 写一个 UILbel的子类:在子类里面重新布置UILbel的字体间距: 如代码 .h #import <UIKit/UIKit.h> @interface AdjustableUI ...

  7. Android ExpandableListView group的item有间距child间隔不变

    <ExpandableListView android:id="@+id/lv" android:layout_width="fill_parent" a ...

  8. [Flex] ButtonBar系列——flex3 ButtonBar各项之间的间距调整

    <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...

  9. zabbix 部分item采集间隔调整

随机推荐

  1. LINUX 笔记-iproute2 命令

    一.IP 地址管理 1.显示 IP 地址 ip addr show [dev eth0] 2.增加删除 IP 地址 ip addr add 192.0.2.1/24 dev eth0 ip addr ...

  2. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  3. 【前端】主流API-promise解析,js基础。

    前言 在js领域,promise出现的时间已经很久了,从jquery的$.get().done().fail() 这样的API开始,到现在的es6默认支持的new Promise(),它的出现无疑使异 ...

  4. 高性能 Lua 技巧(译)

    高性能 Lua 技巧(译) 来源 https://segmentfault.com/a/1190000004372649 此为 Lua Programming Gems 一书的第二章:Lua Perf ...

  5. IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式)

    之前写了一篇文章:<IdentityServer4 实现 OpenID Connect 和 OAuth 2.0> 上面这篇文章虽然详细,但都是点到为止的介绍,并没有实际应用的示例,所以,后 ...

  6. Log4j – Log4j 2 API

    Overview The Log4j 2 API provides the interface that applications should code to and provides the ad ...

  7. Android 开发笔记___FrameLayout

    xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:la ...

  8. 快速学会使用Fiddler抓包 截包伪造提交包

    1.Fiddler介绍 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑,移动设备和互联网之间的http通讯,设置断点,查看所有的"进出"Fiddler的 ...

  9. vue初级学习--环境搭建

    一.导语 最近总想学点东西,es6啊.typescript啊,都想学,刚好有个机遇,可以学点vue,嗯,那就开始吧. 二.正文 1.node环境: 下载安装nodeJs,最好是1.6以上的版本,下载地 ...

  10. 万能日志数据收集器 Fluentd - 每天5分钟玩转 Docker 容器技术(91)

    前面的 ELK 中我们是用 Filebeat 收集 Docker 容器的日志,利用的是 Docker 默认的 logging driver json-file,本节我们将使用 fluentd 来收集容 ...