Adding Images to UIActionSheet buttons as in UIDocumentInteractionController
Solution 1:
There is a possibility to add images (to be exact: icons or symbols) to the buttons of a UIActionSheet
(or a UIAlertView
) without loading image files or fiddling around with (sub)views. In these classes buttons are specified by their titles, which are strings. So it is obvious to use symbols, which one can specify also by strings. The first I came up with was using unicode symbols.
Then I discovered that several of them are rendered as nice icons on iOS and as one can see for several symbols in the Character Viewer on Mac OS, too. Thus, the symbols can be used actually everywhere a string can be specified.
The drawbacks of this approach are:
- You are limited to predefined symbols.
- Not all symbols are rendered as they should (e.g.
\u29C9
). - There can be changes in the appearance of some symbols on different iOS versions (e.g.
\U0001F533
on iOS 5 and 6).
Here are some interesting symbols among others:
- Miscellaneous Symbols and Pictographs
- Miscellaneous Symbols
- Transport and Map Symbols
- Dingbats
If you want to quickly check how a symbol looks like (at least on Mac OS), you can use the Calculator. Check definitely in the simulator: For instance \u2B1C
is not an icon in Calculator 10.7.1.
Screenshots:
UIActionSheet
Button titles:
@"\U0001F6A9 \U0001F4CC \u26F3 \u2690 \u2691 \u274F \u25A4 Test"
@"\U0001F4D6 \U0001F30E \U0001F30F \u25A6 \U0001F3C1 \U0001F332 \U0001F333 \U0001F334 Test"
UIAlertView
Button title:
@"\u26A0 Yes"
UITableViewCell
with checkbox and other icons
Solution 2:
Try this way, i hope it may be help you.
UIActionSheet * action = [[UIActionSheet alloc]
initWithTitle:@"Title"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"",nil];
[[[action valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[[[action valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"yourImage_Highlighted.png"] forState:UIControlStateHighlighted];
Solution 3:
The standard UIActionSheet doesn't support images.
One way to add an image to the UIActionSheet
is to add a subview to the UIActionSheet
. Just implement the UIActionSheetDelegate
method willPresentActionSheet: like this:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
UIImageView* buttonImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picturename.png"]];
// Set the frame of the ImageView that it's over the button.
[actionSheet addSubview:buttonImage];
[buttonImage release]; // only if you don't need this anymore
}
I'm not sure if the image responds to touches, but you can build a UIActionSheet
like theUIDocumentInteractionController
.