将图像按钮放在iPad工具栏的右上角

时间:2012-09-05 21:41:01

标签: ios uitoolbar

我似乎无法在工具栏的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;
}

1 个答案:

答案 0 :(得分: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确实没有显示。