Adding Images to UIActionSheet buttons as in UIDocumentInteractionController
Solution 1
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 2
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:
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 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
.
Solution 4
Here is ones way to do it:
https://github.com/levey/LeveyPopListView
Solution 5
- (IBAction)actionSheetButtonPressed:(id)sender {
UIAlertController * view= [UIAlertController
alertControllerWithTitle:@"Share "
message:@"Select your current status"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* online = [UIAlertAction
actionWithTitle:@"Facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* offline = [UIAlertAction
actionWithTitle:@"Google+"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* doNotDistrbe = [UIAlertAction
actionWithTitle:@"LinkedIn"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* away = [UIAlertAction
actionWithTitle:@"Twitter"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[online setValue:[[UIImage imageNamed:@"facebook.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[offline setValue:[[UIImage imageNamed:@"google-plus.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[doNotDistrbe setValue:[[UIImage imageNamed:@"linkedin.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[away setValue:[[UIImage imageNamed:@"twitter.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[view addAction:online];
[view addAction:away];
[view addAction:offline];
[view addAction:doNotDistrbe];
[view addAction:cancel];
[self presentViewController:view animated:YES completion:nil];
}
Ahmad Kayyali
Software Engineer @ Samsung, sometimes musician & guitar teacher
Updated on January 28, 2020Comments
-
Ahmad Kayyali over 4 years
Is it possible to add an image to the buttons of the
UIActionSheet
as seen inUIDocumentInteractionController
? If so, please let me know how it is done.