我环顾四周,似乎无法在我的工具栏中使用自定义图像。我的 ViewController.m 的自定义初始化部分中有以下代码,这是添加它的正确位置吗?我在上方工具栏中没有看到任何图像。我想把它放在视图的右上方。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
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];
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:barButtonItem];
self.toolbarItems = items;
}
return self;
}
感谢您的关注。