1

我在工具栏上添加了自定义 UIbarButtonItems。一切都是通过 NIB 文件创建的。我看到图像被拉伸了。我的图像是 20x20 和 40x40 的视网膜。它们是 png 文件,我确保它们不包含任何阴影并使用抗锯齿。目前工具栏看起来像这样。 在此处输入图像描述

如果您看到,相同的图像在不添加为 barbuttonItem 时会显得清晰,而只是添加为 imageview。我怎样才能解决这个问题。

4

1 回答 1

1

你可以尝试UIBarButtonItems在你的 viewDidLoad 方法中创建你的吗?

这是我对 a 的处理方式UINavigationBar,所有图像看起来都很好:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:@"img1.png"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];


UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *leftButtonImage = [UIImage imageNamed:"img2.png"];
[leftButton setImage:leftButtonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(10.0, 7.0, leftButtonImage.size.width, leftButtonImage.size.height);
[leftButton addTarget:self action:@selector(method2) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];

method1并且method2是无效方法:-(void)method

并在右侧添加 2 个按钮,我执行以下操作:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:@"img3"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:@selector(method3) forControlEvents:UIControlEventTouchUpInside];

UIButton *middleRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *middleRightButtonImage = [UIImage imageNamed:@"img4"];
[middleRightButton setImage:middleRightButtonImage forState:UIControlStateNormal];
middleRightButton.frame = CGRectMake(0.0, 7.0, middleRightButtonImage.size.width, middleRightButtonImage.size.height);
[middleRightButton addTarget:self action:@selector(method4) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease], [[[UIBarButtonItem alloc] initWithCustomView:middleRightButton] autorelease], nil];

因为UIToolbar你必须添加这样的项目:

[toolbar setItems:[NSArray arrayWithObject:item1, item2, item3, item4, nil]];
于 2013-07-15T14:12:30.213 回答