将图像添加到 UIActionSheet 按钮,如 UIDocumentInteractionController 中一样
是否可以将图像添加到 UIActionSheet
的按钮,如 UIDocumentInteractionController
中所示?如果是这样,请告诉我它是如何完成的。
Is it possible to add an image to the buttons of the UIActionSheet
as seen in UIDocumentInteractionController
? If so, please let me know how it is done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
可以将图像(准确地说:图标或符号)添加到
UIActionSheet
(或UIAlertView
)的按钮,而无需加载图像文件或摆弄 (子)视图。在这些类中,按钮由其标题(字符串)指定。因此,使用符号是显而易见的,也可以通过字符串来指定。我想到的第一个方法是使用 unicode 符号。然后我发现其中有几个在 iOS 上呈现为漂亮的图标,并且可以在 Character 中看到几个符号Mac OS 上的查看器也是如此。因此,这些符号实际上可以在任何可以指定字符串的地方使用。
这种方法的缺点是:
\u29C9
)。\U0001F533
)。以下是一些有趣的符号:
如果您想快速检查符号的外观(至少在 Mac OS 上),您可以使用计算器 。在模拟器中进行明确检查:例如
\u2B1C
不是计算器 10.7.1 中的图标。屏幕截图:
UIActionSheet
按钮标题:
UIAlertView
按钮标题:
UITableViewCell
带有复选框和其他图标There is a possibility to add images (to be exact: icons or symbols) to the buttons of a
UIActionSheet
(or aUIAlertView
) without loading image files or fiddling around with (sub)views. In these classes buttons are specified by their titles, which are strings. So it is obvious to use symbols, which one can specify also by strings. The first I came up with was using unicode symbols.Then I discovered that several of them are rendered as nice icons on iOS and as one can see for several symbols in the Character Viewer on Mac OS, too. Thus, the symbols can be used actually everywhere a string can be specified.
The drawbacks of this approach are:
\u29C9
).\U0001F533
on iOS 5 and 6).Here are some interesting symbols among others:
If you want to quickly check how a symbol looks like (at least on Mac OS), you can use the Calculator. Check definitely in the simulator: For instance
\u2B1C
is not an icon in Calculator 10.7.1.Screenshots:
UIActionSheet
Button titles:
UIAlertView
Button title:
UITableViewCell
with checkbox and other icons试试这个方法,希望对你有帮助。
Try this way, i hope it may be help you.
标准 UIActionSheet 不支持图像。
将图像添加到
UIActionSheet
的一种方法是将子视图添加到UIActionSheet
。只需实现UIActionSheetDelegate
方法 willPresentActionSheet: 就像这样:我不确定图像是否响应触摸,但您可以构建一个
UIActionSheet
就像UIDocumentInteractionController.
The standard UIActionSheet doesn't support images.
One way to add an image to the
UIActionSheet
is to add a subview to theUIActionSheet
. Just implement theUIActionSheetDelegate
method willPresentActionSheet: like this:I'm not sure if the image responds to touches, but you can build a
UIActionSheet
like theUIDocumentInteractionController
.这是一种方法:
https://github.com/levey/LeveyPopListView
Here is ones way to do it:
https://github.com/levey/LeveyPopListView
}
}
您可以通过键“_buttons”从actionSheet对象中获取操作按钮标题并设置按钮图像。
You can get action button title from actionSheet object by key "_buttons" and set button image.
我刚刚创建了一个类,使用支持每行图像和文本的表格单元格来模拟 UIActionSheet 的外观。它还使用块进行交互,支持 iPhone 和 iPad,从 iPad 上的 UITabBarItem 弹出以及多个工作表排队。仍在开发中,但请随意从 Github 克隆它:
http://github.com/azplanlos/SIActionSheet
使用方法非常简单,这是一个示例:
如果您遇到任何问题,请告诉我。我希望这可以帮助很多像我一样遇到同样问题的人...
I just created a class emulating the look of an UIActionSheet using table cells supporting images and text for every row. It also uses blocks for interaction, supports iPhone and iPad, popup from an UITabBarItem on iPad and queueing of multiple sheets. Still in development, but feel free to clone it from Github:
http://github.com/azplanlos/SIActionSheet
Usage is quite simple, here is an example:
If you encounter any problems, please let me know. I hope this helps a lot of people having the same problem like me...
对于 iOS 8,请参考此
For iOS 8, do refer to this
我知道这是很晚的答案,但我找到了另一种在操作表中显示图像的方法:
I know it's very late answer, but I found another way to show image in action sheet:
我发现这个类别扩展在 ios7.1 中工作,可以将图像/图标添加到 UIActionSheet 中的按钮,但有一些警告......
并使用它:
警告:
虽然 UIActionSheet 能够正确地将图像自动调整到按钮的正确高度,但它似乎并没有相应地更改图像视图宽度;因此需要 UIViewContentModeScaleAspectFit 来防止图像被压扁。但是,图像视图框架宽度仍然是原始的全尺寸,因此如果您的图像很大(或更准确地说是宽),那么您会在居中(缩小)的图像和按钮文本之间出现恼人的间隙。我没有找到解决这个问题的方法;即使以编程方式向图像视图添加显式宽度=高度约束似乎也会被忽略!? [有什么想法吗?]。最终结果是,请确保您的图像一开始就具有正确的高度(例如,iPhone 4S 上约为 45 像素),否则按钮图像和文本之间的间隙会越来越大。
更严重的是,只要您向按钮添加图像,UIActionSheet 似乎就会自动使按钮的文本加粗(!)。我不知道为什么,也不知道如何防止这种情况[有什么想法吗?]
最后,此解决方案依赖于 UIActionSheet 的子视图与按钮索引的顺序相同。对于少数按钮来说这是正确的,但是(显然)当你的 UIActionSheet 中有很多项目时,Apple 会搞乱索引 [但是你无论如何都会在 actionSheet:clickedButtonAtIndex: 中遇到问题:当你试图弄清楚时点击了哪个按钮...]
哦,imageEdgeInsets:是可选的 - 我将每个图像插入按钮内几个像素,以便图像不会垂直相互接触。
[观点:鉴于上述奇怪之处,我感觉苹果真的不希望人们乱搞他们的操作表。在某些时候,您可能不得不硬着头皮实现自己的模式弹出窗口;这些 UIActionSheets 只能容纳这么多的人工处理......]
I found this category extension works in ios7.1 to add an image/icon to the buttons in a UIActionSheet, with some caveats...
And to use it:
The caveats:
Although the UIActionSheet does correctly autosize your image to the right height for the button, it does not appear to correspondingly change the imageview width; hence the need for the UIViewContentModeScaleAspectFit to prevent the image from getting squished. However, the imageview frame width is still the original full-size, so if your image was big (or more precisely wide) then you'll get an annoying gap between the centered (shrunk) image and the button text. I've found no way around this; even programmatically adding an explicit width=height constraint to the imageview seems to be ignored!? [any ideas?]. Net outcome, make sure your image is about the right height to begin with (eg about 45 pixels on a iPhone 4S) or you'll get an increasingly large gap between the button image and text.
More serious, as soon as you add an image to the button, the UIActionSheet seems to automatically cause the button's text to be bolded (!). I dont know why and dont know how to prevent this [any ideas?]
Lastly, this solution relies on the UIActionSheet's subviews to be in the same order as the button are indexed. This is true for a handful of buttons, but (apparantly) when you have a lot of items in your UIActionSheet Apple mucks about with the indexing [but you'll have problems with this anyway in actionSheet:clickedButtonAtIndex: when you try to figure out which button was tapped...]
Oh, the imageEdgeInsets: is optional - I inset each image a couple pixels inside the button so that the images dont touch each other vertically.
[Opinion: given the above oddities, I get the feeling Apple really doesn't want people mucking about with their action sheets. At some point you'll probably have to bite-the-bullet and just implement your own modal popup; there's only so much manhandling these UIActionSheets will accommodate...]
从 iOS 8.0 开始,您可以使用
UIAlertController
。在UIAlertController
中,每个按钮项都被称为UIAlertAction
,它会相应地添加。您可以查看我的答案
From iOS 8.0 you can use
UIAlertController
. InUIAlertController
, each button item is know asUIAlertAction
which add accordingly.You can check my answer