0

There are lots of posts on navigation bar but i need to resize it.

Means in my application it get bigger with each view. But i want to keep it smaller in size with all views.

How can i resize that and if possible put an image.

1 Answer 1

1

To resize a navbar button -- or to customize it in any way -- you need to create a custom UIBarButtonItem to add to the navbar.

The following code snippet will create a customized UIBarButtonItem that contains a custom button with normal and highlighted images on it and is sized to be the same size as the image:

UIButton *customButton = nil;
UIImage  *buttonImage = nil;
UIImage  *pressedButtonImage = nil;

buttonImage = [UIImage imageNamed:@"button_image"];
pressedButtonImage = [UIImage imageNamed:@"button_pressed_image"];
customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage : buttonImage forState : UIControlStateNormal];
[customButton setImage : pressedButtonImage forState : UIControlStateHighlighted];
[customButton addTarget : self action : @selector(buttonTapped) forControlEvents : UIControlEventTouchUpInside];
customButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

UIView *container = [[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, buttonImage.size.width, buttonImage.size.height}];
container.backgroundColor = [UIColor clearColor];
[container addSubview:customButton];

UIBarButtonItem *customToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:container];

// add the custom button to the toolbar
self.navigationBar.topItem.rightBarButtonItem = self.addButtonItem;
Sign up to request clarification or add additional context in comments.

4 Comments

Hi,Thanks for your reply. i put your code in my rootviewcontroller class's viewDidLoad() method. UIBarButtonItem *customToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:container]; it says unused variable customToolbarButton and does nothing.
Raj -- you have to add the customBoolbarButton to your toolbar.
i am making this button for navigation bar left hand side one. clicking which takes you one view left.
//back button UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil]; self.navigationItem.backBarButtonItem = backButton; [backButton release];

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.