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 に答える