有可能以某种方式将缩略图icn添加到操作表吗?

时间:2015-02-01 18:18:53

标签: ios objective-c uiactionsheet uiactionsheetdelegate

我想创建带有两个按钮的操作表,但我希望按钮有缩略图标,这可能吗?还有其他方法吗?也许有一种方法可以自定义动作表按钮..喜欢单独设计它们吗?

这是我目前的行动表方法:

- (void)tapResponder {

    NSLog(@"Tap!");

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"this is a title"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"one", @"two", nil];
    [actionSheet showInView:self.view];
}

但我真的很想留下ios设计所以请给我一个很酷的解决方案:))

thankssss!

2 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

for (UIView *subview in actionSheet.subviews) {
    if( [subview isKindOfClass: [UIButton class]] ){
        UIButton* btn = (UIButton*) subview;
        [btn setImage:[UIImage imageNamed: @"imageName"] forState:UIControlStateNormal];
    } 
}

答案 1 :(得分:0)

非常可能,非常容易实现:

这也适用于新的UIAlertController:

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerSytleActionSheet];

//Add your Actions
...
UIAlertAction *snapshot = [UIAlertAction actionWithTitle:@"Snapshot" style:UIAlertActionSytleDefault handler:^(UIAlertAction *action) {
}
//create a UIImage to display since it's technically a tableView:
UIImage *snapshotImage = [UIImage imageNamed:@"snapshot.png"];
//add it to the UIAlertAction of your choosing 
[snapshot setValue:snapshotImage forKey:@"image"];
...
[alertC addAction:snapshot];
[self presentViewController:alertC animated:YES completion:nil];

在iOS 8之前,可以这样做:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"this is a title"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"one", @"two", 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];

[actionSheet showInView:self.view];

有关这方面的一些注意事项,请确保正确调整图像大小,无论H x W维度是什么,一旦将行插入操作表,该行将变得多大。所以使用像30x30这样的东西。此外,无论您的图像是什么颜色,都将默认为蓝色。此外,这不被视为公共API,应用程序确实以这种方式获得批准,但您确实存在风险