带有图像的 UIActionSheet

发布于 2024-12-17 12:32:00 字数 549 浏览 1 评论 0原文

看在上帝的份上,有人告诉我如何在 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;

我尝试更改子视图和工作表的 contentMode。没用。我做错了什么? 在此处输入图像描述

图片应适合按钮之间,或通过任何其他方式以某种方式适合纸张

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城°AllureLove 2024-12-24 12:32:00

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

在此处输入图像描述

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

泪痕残 2024-12-24 12:32:00

UIActionSheet 不支持此类型的自定义。您可以子类化 UIActionSheet 并使用默认布局(覆盖layoutSubviews,调用超级实现,然后移动内容)。如果您这样做,并且 Apple 更改了框架实现,则无法保证您的子类能够在未来版本的 iOS 中工作。

另一种选择是找到或实现一个可以完成您想要的操作的替代类。

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.

柏林苍穹下 2024-12-24 12:32:00

实际上,这很简单,您不需要破解:

在调用 showInView (或 showFromTabBar 等)后更改 UIActionSheet 的大小,就像他们在 这个例子

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

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

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).

五里雾 2024-12-24 12:32:00

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

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];    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文