Adding Images to UIActionSheet buttons as in UIDocumentInteractionController

后端 未结 12 1532
我寻月下人不归
我寻月下人不归 2020-11-30 19:39

Is it possible to add an image to the buttons of the UIActionSheet as seen in UIDocumentInteractionController? If so, please let me know how it is

相关标签:
12条回答
  • 2020-11-30 19:53

    I just created a class emulating the look of an UIActionSheet using table cells supporting images and text for every row. It also uses blocks for interaction, supports iPhone and iPad, popup from an UITabBarItem on iPad and queueing of multiple sheets. Still in development, but feel free to clone it from Github:

    http://github.com/azplanlos/SIActionSheet

    Usage is quite simple, here is an example:

    SIActionSheet* mySheet = [SIActionSheet actionSheetWithTitle:@"Action Sheet title"
        andObjects:[NSArray arrayWithObjects:
            [SIActionElement actionWithTitle:@"Item 1"
                image:[UIImage imageNamed:@"image"]
                andAction:^{NSLog(@"action 1");}]
        , nil]
        completition:^(int num) {
            NSLog(@"pressed %i", num);
        } cancel:^{NSLog(@"canceled");}];
    
    mySheet.followUpSheet = anotherSheet;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
            [mySheet show];
    else
            [mySheet showFromTabBarItem:item inTabBar:tabBar];
    

    If you encounter any problems, please let me know. I hope this helps a lot of people having the same problem like me...

    0 讨论(0)
  • 2020-11-30 19:53

    From iOS 8.0 you can use UIAlertController. In UIAlertController, each button item is know as UIAlertAction which add accordingly.

    You can check my answer

    0 讨论(0)
  • 2020-11-30 19:56
        NSString* strUrl=[MLControl shared].currentServerUrl;
        for( MLServerUrl *title in [MLControl shared].arrServerUrl)  {
            NSString* strShow=title.name;
            if ([strUrl isEqualToString: title.url]) {
                strShow=[NSString stringWithFormat:@"√ %@",strShow];
            }else{
                strShow=[NSString stringWithFormat:@"  %@",strShow];
            }
    
            [chooseImageSheet addButtonWithTitle:strShow];
        }
    
       // [[[chooseImageSheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"ic_check_black_18dp.png"] forState:UIControlStateNormal];
        chooseImageSheet.actionSheetStyle = UIActionSheetStyleDefault;
        [chooseImageSheet showFromRect:btnRc inView:sender animated:YES];
    
    0 讨论(0)
  • 2020-11-30 19:58

    I know it's very late answer, but I found another way to show image in action sheet:

    self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image:" delegate:self cancelButtonTitle:@"Cancel"destructiveButtonTitle:nil otherButtonTitles: @"Image1", @"Image2", @"Image3", @"Image4", @"Image5", @"Image6", @"Image7", @"Image8",@"Image9", @"Image10", @"Image11", @"Image12", @"Image13", @"Image14", @"Image15", nil];
    
    self.actionSheet.tag = 1;
    for (id button in [self.actionSheet valueForKey:@"_buttons"])
     {
         UIImageView* buttonImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[button titleForState:UIControlStateNormal]]];
         [buttonImage setFrame:CGRectMake(5, 5,35,35)];
         [button addSubview:buttonImage];
     }
    
     [self.actionSheet showInView:[UIApplication sharedApplication].keyWindow];
    
    0 讨论(0)
  • 2020-11-30 20:05

    You can get action button title from actionSheet object by key "_buttons" and set button image.

    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Twitter", @"Google +", @"E - mail", @"Send Message",nil];
    
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"fb_icon1.png"] forState:UIControlStateNormal];
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"tweet_icon1.png"] forState:UIControlStateNormal];
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:2] setImage:[UIImage imageNamed:@"googleplus_icon1.png"] forState:UIControlStateNormal];
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:3] setImage:[UIImage imageNamed:@"mail_icon.png"] forState:UIControlStateNormal];
    [[[actionSheet valueForKey:@"_buttons"] objectAtIndex:4] setImage:[UIImage imageNamed:@"message_icon.png"] forState:UIControlStateNormal];
    
    for (UIView *subview in actionSheet.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        }
    }
    
    [actionSheet showInView:self.view];
    
    0 讨论(0)
  • 2020-11-30 20:08

    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.

    0 讨论(0)
提交回复
热议问题