Is it possible to have a UIBarButtonItem as a cell accessoryView?
I have a tableview, where one of the cells is supposed to display a thumbnail image. 开发者_运维技巧However, if that image is not available (hasn't been taken yet), I'd LIKE for the cell's accessoryView to display the camera UIBarButtonItem...but when I try my usual approach I get 'incompatible types' warnings. Is there any way to use those items outside of a nav bar?
It is not possible to directly use UIBarButtonItem
as the cell's accessoryView, because UIBarButtonItem
is not a UIView
.
A suboptimal solution could be to use a UIToolbar
to hold your UIBarButtonItem
. You can then add the UIToolbar
as the cell's accessoryView, but you'll end up with the unwanted outline of the UIToolbar
. This SO answer explains how to make a transparent UIToolbar
subclass. That way you won't see the toolbar's background, but you'll also lose the outline of the UIBarButtonItem
- it will only show the white camera icon:
UIToolbar *toolbar = [[TransparentToolbar alloc] init];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(someAction)];
toolbar.frame = CGRectMake(0, 0, 40, 30);
[toolbar setItems:[NSArray arrayWithObject:item] animated:NO];
cell.accessoryView = toolbar;
[item release];
[toolbar release];
I think that is impossible. Since it is called BarButtonItem it can only go into a bar. The old method allthough works: Take a screenshot of the camera button (just add it to any navigationbar for the screenshot), cut it out from the screenshot, add a regular UIButton to your cell and add the image of your camera button as backgroundimage for your UIButton using
[button setBackgroundImage:[UIImage imageWithImage:@"camerabutton.png"] forState:UIControlStateNormal];
And also take a screenshot of the button when its pressed and save it and set it as image for selected state:
[button setBackgroundImage:[UIImage imageWithImage:@"camerabuttonpressed.png"] forState:UIControlStateHighlitedl];
精彩评论