How to resize navigation bar button
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.
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;
精彩评论