如何处理iOS 11导航BarButtonItems错误? [英] How to handle iOS 11 Navigation BarButtonItems bug?

查看:50
本文介绍了如何处理iOS 11导航BarButtonItems错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS 11中发现了一个UINavigationBar错误,使用self.navigationItem.rightBarButtonItems = @[fixSpaceItem, item]在viewDidLoad中首先设置了导航项按钮.并使用手势弹出,但是我并没有真正弹出,当弹出开始时,我松开手指,让视图控制器取消弹出,然后右导航按钮项消失.左侧的项目按钮有相同的问题.要重现此错误,您必须添加[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]之类的fixSpaceItem.和真实的设备,但不是模拟器可以重现该错误. 这是我的主要代码:

I found a UINavigationBar bug in iOS 11.Fisrtly setup navigation item button in viewDidLoad with self.navigationItem.rightBarButtonItems = @[fixSpaceItem, item]. And use gesture pop back, but I don't really pop back, when pop back starts, I release my finger and let the viewcontroller cancel pop back, then right navigation button items disappear. The left item buttons have the same problem. To reproduce this bug, you must add fixSpaceItem like [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]. And real device but not simulator can reproduce the bug. Here is my main code:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;

    self.navigationItem.rightBarButtonItems = @[[self negativeSpacerWithWidth:5],[self rightButton]];
    self.navigationItem.leftBarButtonItems = @[[self negativeSpacerWithWidth:5], [self leftButton]];

}

- (UIBarButtonItem *)leftButton {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    [button setImage:[UIImage imageNamed:@"icon_app_back_normal"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}

- (UIBarButtonItem *)rightButton {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    [button setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}

- (UIBarButtonItem *)negativeSpacerWithWidth:(CGFloat)width {
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    [spacer setWidth:width];
    return spacer;
}

推荐答案

FixedSpace BarButtonItem添加到BarButtonItem array时似乎是一个错误.如果要为导航项设置偏移量,则可能必须使用另一种方法,例如更改按钮的imageEdgeInsets.

It seems a bug when you add the FixedSpace BarButtonItem to BarButtonItem array. If you want to set offsets to navigation item, may have to use another way, such as changing imageEdgeInsets of button.

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do not set Fixed Space type button item.
    self.navigationItem.leftBarButtonItem = [self leftButton];
    self.navigationItem.rightBarButtonItem = [self rightButton];

    // It work too
    //self.navigationItem.leftBarButtonItems = @[[self leftButton], [self leftButton]];
    //self.navigationItem.rightBarButtonItems = @[[self rightButton], [self rightButton]];


    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (UIBarButtonItem *)leftButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    //...

    // to add offset you want
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}

- (UIBarButtonItem *)rightButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
    //...

    // to add offset you want
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}

这篇关于如何处理iOS 11导航BarButtonItems错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆