iOS11 UIBarButtonItem无法偏移问题坑

1、创建UINavigationItem Category

@implementation UINavigationItem (SXFixSpace)

+(void)load {
    [self swizzleMethod:@selector(setLeftBarButtonItem:)
                                 swizzledSelector:@selector(sx_setLeftBarButtonItem:)];
    [self swizzleMethod:@selector(setRightBarButtonItem:)
                                 swizzledSelector:@selector(sx_setRightBarButtonItem:)];
}

-(void)sx_setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem{
    if (leftBarButtonItem.customView) {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11) {
            UIView *customView = leftBarButtonItem.customView;
            BarView *barView = [[BarView alloc]initWithFrame:customView.bounds];
            [barView addSubview:customView];
            customView.center = barView.center;
            [barView setPosition:SXBarViewPositionLeft];
            [self setLeftBarButtonItems:nil];
            [self sx_setLeftBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:barView]];
        }else {
            [self sx_setLeftBarButtonItem:nil];
            [self setLeftBarButtonItems:@[[self fixedSpaceWithWidth:-20], leftBarButtonItem]];
        }
    }else {
        [self setLeftBarButtonItems:nil];
        [self sx_setLeftBarButtonItem:leftBarButtonItem];
    }
}

-(void)sx_setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem{
    if (rightBarButtonItem.customView) {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11) {
            UIView *customView = rightBarButtonItem.customView;
            BarView *barView = [[BarView alloc]initWithFrame:customView.bounds];
            [barView addSubview:customView];
            customView.center = barView.center;
            [barView setPosition:SXBarViewPositionRight];
            [self setRightBarButtonItems:nil];
            [self sx_setRightBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:barView]];
        } else {
            [self sx_setRightBarButtonItem:nil];
            [self setRightBarButtonItems:@[[self fixedSpaceWithWidth:-20], rightBarButtonItem]];
        }
    }else {
        [self setRightBarButtonItems:nil];
        [self sx_setRightBarButtonItem:rightBarButtonItem];
    }
}

-(UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width {
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                               target:nil
                                                                               action:nil];
    fixedSpace.width = width;
    return fixedSpace;
}

@end

2、创建UIView

typedef NS_ENUM(NSInteger, SXBarViewPosition) {
    SXBarViewPositionLeft,
    SXBarViewPositionRight
};
@interface BarView : UIView
@property (nonatomic, assign) SXBarViewPosition position;
@property (nonatomic, assign) BOOL applied;
@end

@implementation BarView

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.applied || [[[UIDevice currentDevice] systemVersion] floatValue]  < 11) return;
    UIView *view = self;
    while (![view isKindOfClass:UINavigationBar.class] && view.superview) {
        view = [view superview];
        if ([view isKindOfClass:UIStackView.class] && view.superview) {
            if (self.position == SXBarViewPositionLeft) {
                for (NSLayoutConstraint *constraint in view.superview.constraints) {
                    if (([constraint.firstItem isKindOfClass:UILayoutGuide.class] &&
                         constraint.firstAttribute == NSLayoutAttributeTrailing)) {
                        [view.superview removeConstraint:constraint];
                    }
                }
                [view.superview addConstraint:[NSLayoutConstraint constraintWithItem:view
                                                                           attribute:NSLayoutAttributeLeading
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:view.superview
                                                                           attribute:NSLayoutAttributeLeading
                                                                          multiplier:1.0
                                                                            constant:0]];
                self.applied = YES;
            } else if (self.position == SXBarViewPositionRight) {
                for (NSLayoutConstraint *constraint in view.superview.constraints) {
                    if (([constraint.firstItem isKindOfClass:UILayoutGuide.class] &&
                         constraint.firstAttribute == NSLayoutAttributeLeading)) {
                        [view.superview removeConstraint:constraint];
                    }
                }
                [view.superview addConstraint:[NSLayoutConstraint constraintWithItem:view
                                                                           attribute:NSLayoutAttributeTrailing
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:view.superview
                                                                           attribute:NSLayoutAttributeTrailing
                                                                          multiplier:1.0
                                                                            constant:0]];
                self.applied = YES;
            }
            break;
        }
    }
}

@end

3、自定义view添加到leftBarButtonItem

        self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:[UIView alloc] init]];

按钮需要在viewWillAppear中进行添加按钮

- (void)viewWillAppear:(BOOL)animated {
}

未知情况,在viewDidLoad设置,页面再次出现时无法渲染

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏c#开发者

ios5开发-UITableView开启编辑功能

该例子添加UITableView编辑功能 具体功能如下 ? 功能很简单但很实用  @implementation AppDelegate @synthesiz...

3567
来自专栏娱乐心理测试

ios 开发常用技巧

1463
来自专栏Alice

效果类似于label从下往上滑(采用uiTableView实现)

首先附上效果图 ? ? 进行描述一下:效果就是类似于是一个竖直方向的滚动视图 并且方向是从下往上  并且能够一直这样循环下去。 代码“ // // ViewC...

2155
来自专栏移动端周边技术扩展

iOS 11 editActionsForRowAtIndexPath

1556
来自专栏一“技”之长

iOS流布局UICollectionView系列七——三维中的球型布局

        通过6篇的博客,从平面上最简单的规则摆放的布局,到不规则的瀑布流布局,再到平面中的圆环布局,我们突破了线性布局的局限,在后面,我们将布局扩展到了...

972
来自专栏DannyHoo的专栏

底牌项目中设置论坛中各个模块头图的代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

973
来自专栏清墨_iOS分享

自学Swift之路(一)UI入手之基本控件

本系列文章都是以有OC基础来写的,所以注释不会写太多,对基础不够了解的同学可能不太适合,另外本系列文章不是以教程式的形式来写,是记录学习过程的,所以不会有多少讲...

83811
来自专栏编程之路

Swift纯代码 UICollectionView 分组显示、Cell圆角、选中变色

上一篇介绍了如何使用swift纯代码构建UIColletionView,本篇继续介绍如何对其分组、设置分组标题、cell 圆角、选中变色。 效果图如下:

5361
来自专栏刘晓杰

RN实现ListView

3639
来自专栏陈满iOS

iOS·UITableView分割线颜色,隐藏,边距(宽度,起点)等设置

2295

扫码关注云+社区