Here is the snapshot:
And the code is here:
UIButton *leftButton =
Add a UIBarButtonSystemItemFlexibleSpace
item in between the items you want to separate.
I had a similar issue and found an option that sufficed in the storyboard. Just click on the navbar item and you should see the x/y position options.
You can use this code,I think this will be helpful to you. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0,0,50,32); [button setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
[backButtonView addSubview:button];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
//barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);
[self.navigationItem setLeftBarButtonItem:barButtonItem];
The height of a standard bar button is 29pt. Do yourself a favor and just shrink leftbutton
's height and navi_register_up.png
's height instead.
You need to create a UIBarButtonItem
with system item UIBarButtonSystemItemFixedSpace
with -5 width which will remove 5 px
padding. Then set leftBarButtonItems
with both fixed space item and your back bar item like this -
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-5];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBarItem,nil];
Here's a Swift 3 version of the most voted answer:
let soundButton = UIBarButtonItem(image:image , style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.soundPressed))
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -5
self.navigationItem.rightBarButtonItems = [negativeSpacer,soundButton]