ios - 如何处理 iOS 11 Navigation BarButtonItems 错误?

标签 ios objective-c iphone ios11

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

- (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 数组 时,这似乎是一个错误。如果要为导航项设置偏移量,可能需要使用其他方式,例如更改按钮的 imageEdgeInsets。

- (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 - 如何处理 iOS 11 Navigation BarButtonItems 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44896043/

相关文章:

ios - UIView.animate 不延迟显示 View

ios - 如何使 subview 调整大小为 UIScrollView ContentSize?

ios - 具有 block 和停止参数的递归方法

iOS 关闭键盘但继续编辑

objective-c - 检查Mac应用程序安装方法

objective-c - 替换 Objective-C Foundation 函数实现

java - Java至 objective-c :Indexof()

ios - 在iPhone中仅使用G.729编解码器进行VOIP调用

iphone - 触摸事件通过透明部分传递给superview

iphone - UILongPressGestureRecognizer 关闭 UIButton 高亮显示