7

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;
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
ryanzhuang
  • 71
  • 1
  • 4
  • I'm seeing the same even without the fixed space. In my case, the nav bar updates to the navigation item of the view controller you are popping to, but never reverts to the current view controller navigation item when you cancel the pop mid-gesture. Hoping it is cleared up in the next beta. – Chad Podoski Jul 28 '17 at 19:28

3 Answers3

3

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;
}
ovo
  • 1,904
  • 13
  • 26
0

I tried resizing the Image to fit it within navigation item and it worked. Although you don't need to actually resize your images, you can use below provided function to resize the Image at runtime.

    UIImage *imgCart = [self imageWithImage:[UIImage imageNamed:@"ic_cart"] scaledToSize:CGSizeMake(35, 35)] ;
    UIButton *btnCart = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [btnCart addTarget:self action:@selector(btnCartClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btnCart setBackgroundImage:imgCart forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCart];



-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
Yahya Ibrahim
  • 251
  • 4
  • 15
0
let hamButtonWidthConstraint = hamButton.widthAnchor.constraint(equalToConstant: 40)

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

hamButtonWidthConstraint.isActive = true

hamButtonHeightConstraint.isActive = true
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Urvish Modi
  • 1,118
  • 10
  • 11