如何处理iOS 11导航BarButtonItems错误?

前端之家收集整理的这篇文章主要介绍了如何处理iOS 11导航BarButtonItems错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在iOS 11中发现了一个UINavigationBar错误.使用self.navigationItem.rightBarButtonItems = @ [fixSpaceItem,item]在viewDidLoad中快速设置导航项按钮.然后使用手势回弹,但我没有回弹,当弹出回来时,我松开手指并让视图控制器取消弹回,然后右侧导航按钮项目消失.左项按钮具有相同的问题.要重现此错误,您必须添加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,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,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,35)];
    //...

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

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

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

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    return item;
}
原文链接:https://www.f2er.com/iOS/331688.html

猜你在找的iOS相关文章