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

时间:2017-07-04 02:21:29

标签: ios objective-c iphone ios11

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

- (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;
}

3 个答案:

答案 0 :(得分:3)

FixedSpace BarButtonItem添加到BarButtonItem array时似乎是一个错误。如果要设置偏移到导航项,可能必须使用其他方式,例如更改按钮的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;
}

答案 1 :(得分:0)

我尝试调整图像大小以使其适合导航项目并且它有效。虽然您不需要实际调整图像大小,但可以使用下面提供的函数在运行时调整图像大小。

static String c, d,e,f;

答案 2 :(得分:0)

let hamButtonWidthConstraint = hamButton.widthAnchor.constraint(equalToConstant: 40)

let hamButtonHeightConstraint = hamButton.heightAnchor.constraint(equalToConstant: 40)

hamButtonWidthConstraint.isActive = true

hamButtonHeightConstraint.isActive = true