【问题标题】:UIActionSheet with an Image带有图像的 UIActionSheet
【发布时间】:2011-11-22 19:48:14
【问题描述】:

看在上帝的份上,有人告诉我如何在 UIActionSheet 上添加图片。

我正在添加它,但不能强制工作表重新拉伸其高度,因此子视图会适合。

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;

我尝试更改 subView 和工作表的 contentMode。没用。我究竟做错了什么?

图片应该适合按钮之间,或者以某种方式通过任何其他方式适合工作表

【问题讨论】:

标签: ios xamarin.ios uiactionsheet


【解决方案1】:

我知道这听起来很愚蠢,但我找到的最简单的解决方案是添加一些虚拟按钮(以节省空间),然后在它们之上添加准确定义框架坐标的 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);

【讨论】:

  • 这样。简单的。作品。如果有人想要更多控制权,请编写自己的行动表。这并不难,但您的解决方案足以应对大多数情况。
  • 这是一个 hack - 但这不是一件坏事,因为无论如何替代方案都会是更复杂的 hack(即,未来有同样的机会破解)。您应该将您的问题标记为已回答,以便其他人在查看类似问题时可以找到您的解决方案。
【解决方案2】:

UIActionSheet 不支持自定义此类型。您可以使用默认布局继承 UIActionSheet 和 muck(覆盖 layoutSubviews,调用超级实现,然后四处移动)。如果您这样做,如果 Apple 更改框架实现,则无法保证您的 sublcas 将在未来的 iOS 版本中工作。

另一种选择是找到或实现一个替代类来满足您的需求。

【讨论】:

  • 不支持是什么意思?可以添加子视图,为什么不能修复sheet的高度?
  • UIActionSheet 内置了布局按钮的行为。除了公共框架中提供的之外,您无权访问该函数或专门访问 UIActionSheet 的任何子视图。您所能做的就是添加子视图,然后按照我的建议通过覆盖这些实现来尝试修改默认行为。
  • 好的.. 那怎么样:我可以添加一个带有图像的按钮,一个具有非标准高度的按钮吗?
  • 我想我可以添加一些“虚拟”按钮(以保留空间),然后在它们的顶部添加我的子视图,这会产生错觉。按钮将隐藏在 ImageView 下......但我想这样做会很糟糕
  • 与其相比,我会尝试覆盖 layoutSubview。您必须遍历 UIActionSheet 中的所有子视图,尝试找到按钮并将它们向上移动以为您的 imageView 腾出空间。您可能还需要调整 UIActionSheet 的框架。如果您发现一些已发布的代码可以做类似的事情,我不会感到惊讶,抱歉不知道有什么可以指出您的。
【解决方案3】:

其实很简单,不需要破解:

在调用 showInView(或 showFromTabBar 等)后更改 UIActionSheet 的大小,就像在 this example 中所做的那样。

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

不幸的是,这在 iPad 上不起作用。但是你可以使用 UIPopoverController。 (提示:如果您不想要箭头,可以将 0 用于 UIPopoverArrowDirection)。

【讨论】:

    【解决方案4】:

    这是 Agzam 的答案,但根据 Marcel W 的建议,它针对最新的 objc 进行了刷新,并且没有背景按钮 hack。它更短,可能更干净。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      相关资源
      最近更新 更多