Compose UIBarButtonItem changes position slightly

2019-03-25 01:45发布

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?

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-25 02:47

I think this is problem of UIBarButtonSystemItemCompose. need some correction from apple developer team. Untill apple don't get resolve this bug. you can create your custom button and set it to rightBarButtonItem using following code.

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"compose.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 53, 31)];

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = barButton;

-(void)buttonAction:(id)sender{
    NSLog(@"Click");
}

Hope this help you.

查看更多
登录 后发表回答