0

我似乎无法在工具栏中的 iPad 右上角放置一个简单的按钮。我在 viewDidLoad 方法中有代码。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.navigationItem.leftBarButtonItem = self.editButtonItem;

/*
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
 */

UIImage *image = [UIImage imageNamed:@"camera_30_30.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];

button.frame = CGRectMake( 0, 0, image.size.width, image.size.height);
button.showsTouchWhenHighlighted = YES;

[button addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];
//UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:self action:@selector(insertNewObject:)];

NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:barButtonItem];
//self.toolbarItems = items;
self.navigationItem.rightBarButtonItem = barButtonItem;
}
4

1 回答 1

0

几乎可以肯定您的图像太大 - 太大的图像会导致一个白色的空框。此外,您的代码中有一个错误(以及我们不需要通过的多余信息:

- (void)viewDidLoad
{
[super viewDidLoad];

UIImage *image = [UIImage imageNamed:@"camera_30_30.png"];
...

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:self action:@selector(myAction)];

NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:barButtonItem];
self.toolbarItems = items; // Better be a toolbar.items = self.toolbarItems;

[items release];
[barButtonItem release]; // why I use ARC - hard to always get this right
}

我刚刚验证了一个 100x200 左右的图像没有显示,但一个 30x30 的图像显示了。

于 2012-09-05T22:02:56.313 回答