marcos
marcos

Reputation: 33

on iOS, how can I shift navigationItem.leftBarButtonItem horizontally right?

A cusomized UINavigationBar requires me to present a customized "back" button, I use navigationItem.leftBarButtonItem = myCustomizedButton, but its position is fixed.

Would anyone be so kind to share how can I shift this button 40pixels to right?

Upvotes: 3

Views: 3754

Answers (2)

Oded Ben Dov
Oded Ben Dov

Reputation: 10397

You can create a containing view that is 40 pixels bigger than your image. Add your image at 40 pixels offset. Add the containing view as the leftBarButtonItem.

Code follows:

// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, barButtonImage.size.width + 40, barButtonImage.size.height)] autorelease];

// Create a custom button with the image
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:barButtonImage forState:UIControlStateNormal];
barUIButton.frame = CGRectMake(40, 0, barButtonImage.size.width, barButtonImage.size.height);
[barUIButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

[containingView addSubview:barUIButton];

// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];

// Add the container bar button
navigationItem.leftBarButtonItem = containingBarButton;

Upvotes: 7

Hugo
Hugo

Reputation: 19

You can add a blank space to your picture that you display on the navBar. I've had the same problem, and it's the only one solution i've found for resolve it. A little bit tricky but it works...

Upvotes: 1

Related Questions