问题描述
||
是否可以像
UIDocumentInteractionController
所示向UIActionSheet
的按钮添加图像?如果是这样,请让我知道它是如何完成的。
解决方法
尝试这种方式,希望对您有所帮助。
UIActionSheet * action = [[UIActionSheet alloc]
initWithTitle:@\"Title\"
delegate:self
cancelButtonTitle:@\"Cancel\"
destructiveButtonTitle:nil
otherButtonTitles:@\"\",nil];
[[[action valueForKey:@\"_buttons\"] objectAtIndex:0] setImage:[UIImage imageNamed:@\"yourImage.png\"] forState:UIControlStateNormal];
[[[action valueForKey:@\"_buttons\"] objectAtIndex:0] setImage:[UIImage imageNamed:@\"yourImage_Highlighted.png\"] forState:UIControlStateHighlighted];
,可以将图像(准确地说是图标或符号)添加到“ 0”(或“ 4”)的按钮上,而无需加载图像文件或摆弄(子)视图。在这些类中,按钮由其标题(即字符串)指定。因此,显然可以使用符号,也可以通过字符串指定符号。我首先想到的是使用unicode符号。
然后我发现其中的一些在iOS上被渲染为漂亮的图标,并且在Mac OS的Character Viewer中也可以看到几个符号。因此,实际上可以在可以指定字符串的任何地方使用符号。
这种方法的缺点是:
您仅限于预定义的符号。
并非所有符号都按应有的方式呈现(例如\\u29C9
)。
在不同的iOS版本(例如,iOS 5和6上为e6ѭ)上,某些符号的外观可能会发生变化。
以下是一些有趣的符号:
其他符号和象形文字
杂项符号
运输和地图符号
丁巴特
如果要快速检查符号的外观(至少在Mac OS上是这样),可以使用计算器。绝对在模拟器中检查:例如,\\u2B1C
不是计算器10.7.1中的图标。
屏幕截图:
UIActionSheet
按钮标题:
@\"\\U0001F6A9 \\U0001F4CC \\u26F3 \\u2690 \\u2691 \\u274F \\u25A4 Test\"
@\"\\U0001F4D6 \\U0001F30E \\U0001F30F \\u25A6 \\U0001F3C1 \\U0001F332 \\U0001F333 \\U0001F334 Test\"
UIAlertView
按钮标题:
@\"\\u26A0 Yes\"
UITableViewCell
带有复选框和其他图标
,标准的UIActionSheet不支持图像。
将图像添加到“ 0”的一种方法是将子视图添加到“ 0”。只需实现UIActionSheetDelegate
方法willPresentActionSheet:就像这样:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
UIImageView* buttonImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"picturename.png\"]];
// Set the frame of the ImageView that it\'s over the button.
[actionSheet addSubview:buttonImage];
[buttonImage release]; // only if you don\'t need this anymore
}
我不确定图像是否响应触摸,但是您可以像build1一样构建build0ѭ。
,这是一种实现方法:
https://github.com/levey/LeveyPopListView
,- (IBAction)actionSheetButtonPressed:(id)sender {
UIAlertController * view= [UIAlertController
alertControllerWithTitle:@\"Share \"
message:@\"Select your current status\"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* online = [UIAlertAction
actionWithTitle:@\"Facebook\"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* offline = [UIAlertAction
actionWithTitle:@\"Google+\"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* doNotDistrbe = [UIAlertAction
actionWithTitle:@\"LinkedIn\"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* away = [UIAlertAction
actionWithTitle:@\"Twitter\"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@\"Cancel\"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[online setValue:[[UIImage imageNamed:@\"facebook.png\"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@\"image\"];
[offline setValue:[[UIImage imageNamed:@\"google-plus.png\"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@\"image\"];
[doNotDistrbe setValue:[[UIImage imageNamed:@\"linkedin.png\"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@\"image\"];
[away setValue:[[UIImage imageNamed:@\"twitter.png\"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@\"image\"];
[view addAction:online];
[view addAction:away];
[view addAction:offline];
[view addAction:doNotDistrbe];
[view addAction:cancel];
[self presentViewController:view animated:YES completion:nil];
}
,您可以通过\\“ _ buttons \”键从actionSheet对象获取操作按钮标题并设置按钮图像。
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@\"Title\" delegate:self cancelButtonTitle:@\"Cancel\" destructiveButtonTitle:nil otherButtonTitles:@\"Facebook\",@\"Twitter\",@\"Google +\",@\"E - mail\",@\"Send Message\",nil];
[[[actionSheet valueForKey:@\"_buttons\"] objectAtIndex:0] setImage:[UIImage imageNamed:@\"fb_icon1.png\"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@\"_buttons\"] objectAtIndex:1] setImage:[UIImage imageNamed:@\"tweet_icon1.png\"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@\"_buttons\"] objectAtIndex:2] setImage:[UIImage imageNamed:@\"googleplus_icon1.png\"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@\"_buttons\"] objectAtIndex:3] setImage:[UIImage imageNamed:@\"mail_icon.png\"] forState:UIControlStateNormal];
[[[actionSheet valueForKey:@\"_buttons\"] objectAtIndex:4] setImage:[UIImage imageNamed:@\"message_icon.png\"] forState:UIControlStateNormal];
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
}
}
[actionSheet showInView:self.view];
,我刚刚创建了一个类,该类使用支持每行图像和文本的表格单元来模拟UIActionSheet的外观。它还使用块进行交互,支持iPhone和iPad,从iPad上的UITabBarItem弹出,并对多张纸进行排队。仍在开发中,请随时从Github克隆它:
http://github.com/azplanlos/SIActionSheet
用法很简单,下面是一个示例:
SIActionSheet* mySheet = [SIActionSheet actionSheetWithTitle:@\"Action Sheet title\"
andObjects:[NSArray arrayWithObjects:
[SIActionElement actionWithTitle:@\"Item 1\"
image:[UIImage imageNamed:@\"image\"]
andAction:^{NSLog(@\"action 1\");}],nil]
completition:^(int num) {
NSLog(@\"pressed %i\",num);
} cancel:^{NSLog(@\"canceled\");}];
mySheet.followUpSheet = anotherSheet;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
[mySheet show];
else
[mySheet showFromTabBarItem:item inTabBar:tabBar];
如果您遇到任何问题,请告诉我。我希望这可以帮助很多像我一样遇到同样问题的人...
,对于iOS 8,请参考此
if( [UIAlertController class] ){
UIAlertController *view = [UIAlertController alertControllerWithTitle:@\"Main Title\"
message:@\"What do you want to do?\"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *firstAA = [UIAlertAction actionWithTitle:@\"Beep Beep\"
style:UIAlertActionStyleDefault
handler:^( UIAlertAction *action ){
[view dismissViewControllerAnimated:YES
completion:nil];
}];
[firstAA setValue:[UIImage imageNamed:@\"your-icon-name\"] forKey:@\"image\"];
[view addAction:firstAA];
UIAlertAction *cancelAA = [UIAlertAction actionWithTitle:@\"Cancel\"
style:UIAlertActionStyleCancel
handler:^( UIAlertAction *action ){
[self deselectTableViewRow];
[view dismissViewControllerAnimated:YES
completion:nil];
}];
[view addAction:cancelAA];
[self presentViewController:view
animated:YES
completion:nil];
}
else {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@\"What do you want to do?\"
delegate:(id)self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[sheet addButtonWithTitle:@\"title\"];
[[[sheet valueForKey:@\"_buttons\"] objectAtIndex:0] setImage:[UIImage imageNamed:@\"your-icon-name.png\"] forState:UIControlStateNormal];
sheet.cancelButtonIndex = [sheet addButtonWithTitle:@\"Cancel\"];
[sheet showInView:self.view];
}
,我知道答案很晚,但是我找到了另一种在操作表中显示图像的方法:
self.actionSheet = [[UIActionSheet alloc] initWithTitle:@\"Select Image:\" delegate:self cancelButtonTitle:@\"Cancel\"destructiveButtonTitle:nil otherButtonTitles: @\"Image1\",@\"Image2\",@\"Image3\",@\"Image4\",@\"Image5\",@\"Image6\",@\"Image7\",@\"Image8\",@\"Image9\",@\"Image10\",@\"Image11\",@\"Image12\",@\"Image13\",@\"Image14\",@\"Image15\",nil];
self.actionSheet.tag = 1;
for (id button in [self.actionSheet valueForKey:@\"_buttons\"])
{
UIImageView* buttonImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[button titleForState:UIControlStateNormal]]];
[buttonImage setFrame:CGRectMake(5,5,35,35)];
[button addSubview:buttonImage];
}
[self.actionSheet showInView:[UIApplication sharedApplication].keyWindow];
,我发现此类别扩展在ios7.1中有效,可以向UIActionSheet中的按钮添加图像/图标,但有一些注意事项...
@interface UIActionSheet (GSBActionSheetButtons)
- (void)buttonAtIndex:(NSUInteger)index setImage:(UIImage *)image forState:(UIControlState)state;
@end
@implementation UIActionSheet (GSBActionSheetButtons)
- (void)buttonAtIndex:(NSUInteger)index setImage:(UIImage *)image forState:(UIControlState)state
{
for (UIView* view in self.subviews) {
if ([view isKindOfClass:[UIButton class]]) {
if (index-- == 0) {
UIButton *button = (UIButton*)view;
[button setImage:image forState:state];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageEdgeInsets = UIEdgeInsetsMake(2,2,0);
break;
}
}
}
}
并使用它:
[self.sharePopup buttonAtIndex:2 setImage:[UIImage imageNamed:@\"twitter.png\"] forState:UIControlStateNormal];
注意事项:
尽管UIActionSheet确实将按钮的图像大小正确正确地自动调整到了正确的高度,但它似乎并未相应地更改imageview的宽度;因此,需要UIViewContentModeScaleAspectFit来防止图像被挤压。但是,imageview框架的宽度仍然是原始的全尺寸,因此,如果您的图像很大(或更精确地说是宽),则在居中(缩小)的图像和按钮文本之间会出现一个烦人的间隙。我找不到解决办法;甚至以编程方式向imageview添加显式width = height约束似乎都被忽略了! [有任何想法吗?]。最终结果,请确保您的图像开始时具有正确的高度(例如,iPhone 4S约为45像素),否则按钮图像和文本之间的间隙将越来越大。
更严重的是,将图像添加到按钮后,UIActionSheet似乎会自动使按钮的文本加粗(!)。我不知道为什么,也不知道如何防止这种情况[任何想法?]
最后,该解决方案依赖于UIActionSheet的子视图,该子视图与索引按钮的顺序相同。这对于少数按钮是正确的,但是(显然)当您的UIActionSheet中有很多项时,Apple会对索引进行嘲笑[但是无论如何,您都会在actionSheet:clickedButtonAtIndex:中遇到问题:按下哪个按钮...]
哦,imageEdgeInsets:是可选的-我将每个图像插入按钮内几个像素,以使图像不会垂直彼此接触。
[观点:鉴于上述怪异之处,我感到苹果真的不希望人们为自己的行动表而烦恼。在某些时候,您可能不得不忍住子弹,然后实施自己的模式弹出窗口;这些UIActionSheets只能处理这么多...]
, NSString* strUrl=[MLControl shared].currentServerUrl;
for( MLServerUrl *title in [MLControl shared].arrServerUrl) {
NSString* strShow=title.name;
if ([strUrl isEqualToString: title.url]) {
strShow=[NSString stringWithFormat:@\"√ %@\",strShow];
}else{
strShow=[NSString stringWithFormat:@\" %@\",strShow];
}
[chooseImageSheet addButtonWithTitle:strShow];
}
// [[[chooseImageSheet valueForKey:@\"_buttons\"] objectAtIndex:0] setImage:[UIImage imageNamed:@\"ic_check_black_18dp.png\"] forState:UIControlStateNormal];
chooseImageSheet.actionSheetStyle = UIActionSheetStyleDefault;
[chooseImageSheet showFromRect:btnRc inView:sender animated:YES];
,在iOS 8.0中,您可以使用UIAlertController
。在UIAlertController
中,每个按钮项都称为UIAlertAction
,并相应增加。
你可以检查我的答案