2011-11-22 55 views
2

为了上帝的缘故,有人告诉我如何在UIActionSheet上添加图片。UIActionSheet with Image

我正在添加它,但不能强制纸张恢复其高度,所以Subview会适合。

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; 

我试图改变子视图和片材的contentMode。没有工作。我究竟做错了什么? enter image description here

图片应按钮之间配合,或通过任何其他适合的方式在片材上以某种方式围绕

+1

你有没有试过这种方法 - http://stackoverflow.com/questions/1305623/add-image-into-uiactionsheet - 使用ImageView? – Jason

+0

@Jason它增加了图片视图,但结果是一样的,它不会拉伸视图的高度 – Agzam

回答

12

我知道这听起来很愚蠢,但我发现的最简单的解决方案是添加一些虚拟按钮(以保留空间),然后在其上添加UIImageView,以准确定义框架坐标。

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

+2

就像那样。简单。作品。如果有人想要更多控制权,请编写自己的操作表。这并不难,但你的解决方案在大多数情况下都足够了。 – Krumelur

+1

这是一种破解 - 但这不是一件坏事,因为无论如何,替代品将会变得更加复杂(即在将来破产的可能性相同)。您应该将问题标记为已回答,以便其他人在查看类似问题时可以找到解决方案。 – poupou

2

UIActionSheet不支持这种类型的定制。您可以继承UIActionSheet,并使用默认布局(覆盖layoutSubviews,调用超级实现,然后移动东西)。如果你这样做,那么如果苹果改变了框架的实现,就不能保证你的sublcass能够在将来的iOS版本中工作。

另一种选择是找到或实现一个可以做你想做的替代类。

+0

你的意思是什么不支持?我可以添加一个子视图,为什么我无法修复纸张的高度? – Agzam

+1

UIActionSheet内置了布局按钮的行为。除了公共框架中提供的UIActionSheet的任何子视图之外,您无权访问该功能或特定访问权限。你所能做的就是添加子视图,然后尝试按照我的建议覆盖这些实现,从而修改默认行为。 – XJones

+0

好的..那怎么样:我可以添加一个带有图像的按钮,非标准高度的按钮吗? – Agzam

2

其实这是很容易的,你并不需要一个黑客:

更改UIActionSheet的大小后调用showInView(或showFromTabBar等),就像他们在做this example

您可能需要更改框架而不是边界,根据我的经验,如果更改界限,则操作表不会移动到正确的位置。

在iPad上,这不起作用。但是你可以使用UIPopoverController。 (提示:如果不需要箭头,可以使用UIPopoverArrowDirection为0)。

1

这是Agzam的回答,但刷新了最近的objc,没有背景按钮破解,正如Marcel W.所建议的那样,它更短,更干净。

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