Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compose UIBarButtonItem changes position slightly when coming into view

When presenting a new view with a UIBarButtonSystemItemCompose button in the navigation bar, the position is slightly off and adjusts after the view has come into view.

Compose button changes position slightly when coming into view

I think this is a bug in iOS (version 8.3 used). It only happens when using the UIBarButtonSystemItemCompose. It does not happen with other types of Buttons (system, text or custom).

The only code needed to replicate this bug is to use this ViewController code with the view that will come into view:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIBarButtonItem* composeBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                      target:nil
                                      action:nil];

    [self.navigationItem setRightBarButtonItem:composeBarButtonItem animated:YES];
}

@end

I have created a repository on GitHub with bare minimum code to reproduce the problem: https://github.com/jvdvleuten/iOSComposeBarButtonItemBug

Looks related to this: UIBarButtonItems shift position when UINavigationController is presented modally, except my bug only appears when using the UIBarButtonSystemItemCompose.

Any ideas?

like image 431
jvdveuten Avatar asked Apr 22 '15 11:04

jvdveuten


2 Answers

I used Sergey's answer, but kept an empty space right of my button. I fixed this with a negative spacer, which now works beautifully:

UIBarButtonItem* composeBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                              target:nil
                                              action:nil];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
negativeSpacer.width = -6;

UIBarButtonItem *dumbBarButtonItem = [UIBarButtonItem new];

self.navigationItem.rightBarButtonItems = @[dumbBarButtonItem, negativeSpacer, self.composeBarButtonItem];
like image 81
jvdveuten Avatar answered Oct 01 '22 09:10

jvdveuten


This is definitely a bug in iOS 8.0. This 'jump' occurs before viewDidAppear. Here is workaround for this - add another 'dumb'/empty item:

UIBarButtonItem* composeBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
                                              target:nil
                                              action:nil];
UIBarButtonItem *dumbBarButtonItem = [UIBarButtonItem new];
self.navigationItem.rightBarButtonItems = @[dumbBarButtonItem, composeBarButtonItem];
like image 29
Sergey Kuryanov Avatar answered Oct 01 '22 09:10

Sergey Kuryanov