Align the self.navigationItem.backBarButtonItem to left of the device in iOS

566 views Asked by At

What I tried was, add an image to UIBarButtonItem and set that UIBarButtonItem as the default backBarButtonItem.

Below is my code;

UIImage *backNavImg = [[UIImage imageNamed:@"backward_navigation_arrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithImage:backNavImg style:UIBarButtonItemStylePlain target:self action:nil];

self.navigationItem.backBarButtonItem = backBtnItem;

The issue is, there is considerable space between that back bar button item and left margin. I have highlighted it from Red colour on the image.

enter image description here

I want to decrease that gap from programmatically. How can I achieve that.

I tried below code, but nothing happened.

[backBtnItem setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
1

There are 1 answers

1
iDhaval On

you can do this by adding leftBarButtonItems

Example:

UIImage *backNavImg = [[UIImage imageNamed:@"backward_navigation_arrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithImage:backNavImg style:UIBarButtonItemStylePlain target:self action:nil];

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -8;
self.navigationItem.leftBarButtonItems=@[negativeSpacer,barButton];