2

For God's sake somebody tell me how to add a picture on an UIActionSheet.

I am adding it, but can't force the sheet to restretch its height, so the Subview would fit.

var sheet = new UIActionSheet ("Sheet with a picture");
sheet.AddButton ("Pick New Picture");

var subView = new UIView(new RectangleF(20,100,280,200)){ 
    BackgroundColor = UIColor.FromPatternImage(Picture)};

sheet.AddSubview(subView);
sheet.AddButton ("Cancel");
sheet.CancelButtonIndex = 1;

I've tried to change contentMode of subView and the sheet. Didn't work. What am I doing wrong? enter image description here

Picture should fit between buttons, or fit on the sheet somehow through any other way around

iLemming
  • 34,477
  • 60
  • 195
  • 309
  • 1
    have you tried this method - http://stackoverflow.com/questions/1305623/add-image-into-uiactionsheet - using an ImageView? – Jason Nov 22 '11 at 20:24
  • @Jason it adds the picture view, but the result is the same, it doesn't stretch the View's height – iLemming Nov 22 '11 at 20:39

4 Answers4

12

I know this sounds really stupid, but the easiest solution I found is to add a few dummy buttons (to preserve space) and then on top of them add the UIImageView accurately defining the frame coordinates.

var sheet = new UIActionSheet ("");
sheet.AddButton ("Discard Picture");
sheet.AddButton ("Pick New Picture");
sheet.AddButton ("Cancel");
sheet.CancelButtonIndex = 2;

// Dummy buttons to preserve the space for the UIImageView
for (int i = 0; i < 4; i++) {
    sheet.AddButton("");
    sheet.Subviews[i+4].Alpha = 0; // And of course it's better to hide them
}

var subView = new UIImageView(Picture);
subView.ContentMode = UIViewContentMode.ScaleAspectFill;
subView.Frame = new RectangleF(23,185,275,210);

// Late Steve Jobs loved rounded corners. Let's have some respect for him
subView.Layer.CornerRadius = 10; 
subView.Layer.MasksToBounds = true;
subView.Layer.BorderColor = UIColor.Black.CGColor;
sheet.AddSubview(subView);

enter image description here

iLemming
  • 34,477
  • 60
  • 195
  • 309
  • 2
    Like that. Simple. Works. If somebody wants more control, write your own action sheet. It's not that hard but your solution suffices for most cases. – Krumelur Nov 23 '11 at 09:22
  • 1
    It's an hack - but that not a bad thing since alternatives would be more complex hacks anyway (i.e. same chance of breaking in the future). You should mark your question as answered so other people can find your solution when looking at a similar problem. – poupou Nov 24 '11 at 13:46
2

Actually it is quite easy and you don't need a hack:

Change the size of the UIActionSheet AFTER calling showInView (or showFromTabBar, etc), like they do in this example.

You might have to change the Frame instead of the Bounds, in my experience the action sheet is not moved to the right position if you change the Bounds.

On the iPad this doesn't work unfortunately. But there you can use a UIPopoverController. (Tip: you can use 0 for the UIPopoverArrowDirection if you do not want arrows).

Community
  • 1
  • 1
Marcel Wolterbeek
  • 3,367
  • 36
  • 48
2

UIActionSheet doesn't support customization of this type. You can subclass UIActionSheet and muck with the default layout (override layoutSubviews, call the super implementation, then move things around). If you do this, there's no guarantee your sublcass will work in future versions of iOS if Apple changes the framework implementation.

The other alternative is to find or implement an alternative class that does what you want.

XJones
  • 21,959
  • 10
  • 67
  • 82
  • what do you mean doesn't support? I can add a subview, why can't I fix the sheet's height? – iLemming Nov 22 '11 at 20:35
  • 1
    UIActionSheet has built in behavior to layout buttons. You have no access to that function or specifically to any subviews of a UIActionSheet other than as provided in the public framework. All you can do is add subviews and then try to modify the default behavior after the fact by overriding those implementations as I suggested. – XJones Nov 22 '11 at 20:42
  • Ok.. how about that: Can I add a button with an image, a button with non-standart height? – iLemming Nov 22 '11 at 20:49
  • I guess I can add a few "dummy" buttons (to reserve the space) and then on the top of them add my subview, that would create the illusion. Buttons would be hidden under the ImageView... But I guess this would be lousy way to do that – iLemming Nov 22 '11 at 20:51
  • 1
    Rather than that, I would try overriding layoutSubview. You'll have to walk through all of the subviews in the UIActionSheet, try to find the buttons and move them up to make room for your imageView. You may also need to adjust the frame of the UIActionSheet. I wouldn't be surprised if you find some published code to do something similar, sorry don't know of any to point you at. – XJones Nov 22 '11 at 20:59
  • That's fine, I didn't recommend that b/c I don't like having the extra buttons around even if you can't see them. Never know if there's some strange case where they'll end up above your image or the user could somehow tap one. – XJones Nov 22 '11 at 23:18
  • that's why you have to hide them, setting Alpha to zero – iLemming Nov 23 '11 at 16:17
1

This is Agzam's answer, but refreshed for most recent objc and without the background button hack, as suggested by Marcel W. it is shorter and possibly cleaner.

 UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
  actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
  actionSheet.title = @"some text";//here type your information text
  actionSheet.delegate = self;

  [actionSheet addButtonWithTitle:@"Button 1"];
  [actionSheet addButtonWithTitle:@"Button 2"];
  [actionSheet setCancelButtonIndex:1];

//it is up to you how you show it, I like the tabbar
  [actionSheet showFromTabBar:self.tabBarController.tabBar];

//here lays the trick - you change the size after you've called show 
  [actionSheet setBounds:CGRectMake(0,0,320, 480)];

//now create and add your image
   UIImageView *subView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"exampleimage.png"]];
   subView.ContentMode = UIViewContentModeScaleAspectFit;
   subView.Frame = CGRectMake(0,130,320,180);//adjust these, so that your text fits in
   [actionSheet addSubview: (subView)];
   [subView release];

  [actionSheet release];    
drpawelo
  • 2,348
  • 23
  • 17