How to display custom view in UIActionSheet?

2019-07-06 04:06发布

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?

4条回答
叼着烟拽天下
2楼-- · 2019-07-06 04:43

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];
查看更多
祖国的老花朵
3楼-- · 2019-07-06 04:44

I do not believe you can specify Custom Views in a UIActionSheet. My understanding is that you can only display a title and 1 or more buttons. Below is the description of UIActionSheet in the Apple documentation:

Use the UIActionSheet class to present the user with a set of alternatives for how to proceed with a given task. You can also use action sheets to prompt the user to confirm a potentially dangerous action. The action sheet contains an optional title and one or more buttons, each of which corresponds to an action to take.

UIActionSheet Class Reference

查看更多
仙女界的扛把子
4楼-- · 2019-07-06 04:49

You might want to check out an UIActionSheet alternative I made which lets you display custom content views without any dirty hacks: https://github.com/JonasGessner/JGActionSheet It has some other advantages over UIActionSheet as well!

查看更多
三岁会撩人
5楼-- · 2019-07-06 05:03

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];

}
查看更多
登录 后发表回答