如何调整导航栏按钮的大小 [英] 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.
推荐答案
要调整导航栏按钮的大小 - 或以任何方式自定义它 - 您需要创建一个自定义 UIBarButtonItem
以添加到导航栏。
To resize a navbar button -- or to customize it in any way -- you need to create a custom UIBarButtonItem
to add to the navbar.
以下代码片段将创建一个自定义的 UIBarButtonItem
,其中包含一个自定义按钮,上面包含正常和突出显示的图像,大小与图像大小相同:
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;
这篇关于如何调整导航栏按钮的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!