0

I am making an iPhone app and I have a UINavigationController as my root view and then UIVIewControllers from this. My Register button segues to the registration screen which automatically creates a Navigation Bar for me with the back button on the top left.

I have replaced the standard back button with my own image with the code below:

UIImage *backImage = [UIImage imageNamed:@"customBackButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 100, 70);
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(popNavigationController:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton] ;
self.navigationItem.leftBarButtonItem = backBarButtonItem;

I have also added a rightBarButtonItem to test but my issue is however that both these buttons arent in the corners of the screen as can be seen here. enter image description here

Most other apps have the navigation bar buttons right in the corners as can be seen here from another app I found online as reference

enter image description here

I have tried to change the values here but that does nothing - the buttons stay where they are.

// Changing frames x-coordinate
backButton.frame = CGRectMake(-220, 0, 100, 70);

What do I need to do to set the buttons right in the corners of the navigation bar?

EDIT: If I use [backButton sizeToFit]; my image is displayed as below (the top grey bar is the navigation bar). Do I need to make the image the correct size?

enter image description here

3 Answers 3

1

If you want to change position of UIBarButtonItem you should use fake bar items as spacers

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

Now you can set the array of bar items to achieve result needed

self.navigationItem.leftBarButtonItems = @[negativeSpacer, backBarButtonItem];
Sign up to request clarification or add additional context in comments.

Comments

0

The width of the buttons is too large. Remove this line:

backButton.frame = CGRectMake(0, 0, 100, 70);

and then call

[backButton sizeToFit];

after

[backButton setImage:backImage forState:UIControlStateNormal];

If this doesn't work, it's possible that there is white space baked into the PNG images themselves.

1 Comment

OK so I have to make the image the correct size? When I implement your solution and place the code as suggested, my button is HUGE and placed right in the middle of the navigation bar. I will edit my question to show yo what I mean
0

Have you tried cropping the image you're using (customBackButton.png) to get rid of any transparent margins? It looks like the image might be a little wide. Then you could decrease the width from 100px to something smaller and things should fit better.

1 Comment

Yes I have checked the image and there are no transparent edges on it at all. The image is cropped right to the edges of the hexagon shape

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.