Search code examples
iosxamarin.iosuiactionsheet

UIActionSheet with an Image


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


Solution

  • 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