How to display custom view in UIActionSheet?

19,617

Solution 1

I've discovered the behavoir occurs because I haven't used the UIActionSheet constructor properly. The buttons and title should be included:

UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

Solution 2

use UIActionSheetDelegate and after that you can with this delegate add for example a image to button of alertsheet

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet{

   UIImageView *sample = [[UIImageView alloc] initWithImage:[UIImage       
   imageNamed:@"Someimage.png"]];
   [actionSheet addSubView:sample];

}
Share:
19,617
4thSpace
Author by

4thSpace

Updated on June 05, 2022

Comments

  • 4thSpace
    4thSpace about 2 years

    I have a UIView with a date picker that I'd like to display in an action sheet. I'm using the following code:

    -(IBAction) button_click:(id)sender{
        //UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"the title" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive" otherButtonTitles:@"other", nil];
    
        UIActionSheet *sheet = [[UIActionSheet alloc] init];
        ActionSheetController *actionSheet = [[ActionSheetController alloc]  initWithNibName:@"ActionSheetView" bundle:nil];    
        [sheet addSubview:actionSheet.view];
    
        [sheet showInView:self.view];
    }
    

    What I get is a little bit of the top part of the new view coming up from the bottom and that's it. If I comment the two middle lines of code and uncomment the top part to display a regular action sheet, it works fine. Any ideas what I might be doing wrong?

  • 4thSpace
    4thSpace about 14 years
    If you take a look at this post stackoverflow.com/questions/2608239/…, I have a link to the zipcar app, which seems to using a custom action sheet.
  • n.dee
    n.dee about 14 years
    Technically what you are trying to do could be possible after all UIActionSheet is just another type of UIView. My concern is that it you may run into problems when you submit the application to Apple as they expect UIActionSheet to be used in a very specific way as described in the iPhone HIG developer.apple.com/iphone/library/documentation/UserExperie‌​nce/… Have you considered creating a custom modal view instead of trying to use UIActionSheet?
  • 4thSpace
    4thSpace about 14 years
    Wouldn't modal view controller hide/disable the navigation bar?
  • n.dee
    n.dee about 14 years
    Yes but so does UIActionSheet. The navigation bar is placed behind a semi-transparent overlay. Why would you want access to the navigation bar when displaying a UIActionSheet? It is designed specifically to force the user to select an action or dismiss the view. What are you trying to achieve with the UIActionSheet that requires a custom view embedded in it? If I had more details I might be able to give a more detailed response.
  • 4thSpace
    4thSpace about 14 years
    take a look at my first comment above about the zipcar app. That's what I'm trying to do.
  • nine stones
    nine stones over 11 years
    @4thSpace: True, but you can embed the modal ViewController in a UINavigationController.
  • mpemburn
    mpemburn over 11 years
    This would be great if correct. Fails with: "No visible @interface for 'UIActionSheet' declares the selector 'addSubView:'"
  • user2277872
    user2277872 almost 11 years
    just lowercase the v..its [actionSheet addSubview:sample];
  • Eric G
    Eric G over 10 years
    You might want to be careful with this. Apple may not like it. From the documentation: "UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:."