Is it possible somehow to add a thumbnail icn to action sheet?

85 Views Asked by At

I want to create action sheet with two buttons, but i want the buttons to have thumbnail icons, is that possible? any other solutions? maybe there is a way to customize the action sheet buttons.. like to design them separately?

this is my current action sheet method:

- (void)tapResponder {

    NSLog(@"Tap!");

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"this is a title"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"one", @"two", nil];
    [actionSheet showInView:self.view];
}

but i really like to stay with ios design so please give me a cool solution :))

thankssss!

2

There are 2 best solutions below

0
3CC On

Maybe this helps:

for (UIView *subview in actionSheet.subviews) {
    if( [subview isKindOfClass: [UIButton class]] ){
        UIButton* btn = (UIButton*) subview;
        [btn setImage:[UIImage imageNamed: @"imageName"] forState:UIControlStateNormal];
    } 
}
3
soulshined On

Very possible, very easy to implement :

This works with the new UIAlertController as well :

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerSytleActionSheet];

//Add your Actions
...
UIAlertAction *snapshot = [UIAlertAction actionWithTitle:@"Snapshot" style:UIAlertActionSytleDefault handler:^(UIAlertAction *action) {
}
//create a UIImage to display since it's technically a tableView:
UIImage *snapshotImage = [UIImage imageNamed:@"snapshot.png"];
//add it to the UIAlertAction of your choosing 
[snapshot setValue:snapshotImage forKey:@"image"];
...
[alertC addAction:snapshot];
[self presentViewController:alertC animated:YES completion:nil];

Prior to iOS 8 it can be done this way:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"this is a title"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"one", @"two", 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];

[actionSheet showInView:self.view];

A couple of notes about this, make sure you properly size your image, whatever the H x W demensions are is how huge the row will become once it's inserted into the Action Sheet. So use something like 30x30. Also, whatever color your image is will default to the blue tint. Additionally, this isn't considered public API, apps do get approved this way, but you do run the risk