【问题标题】:Is it possible somehow to add a thumbnail icn to action sheet?是否可以以某种方式将缩略图icn 添加到操作表?
【发布时间】:2015-02-01 18:18:52
【问题描述】:

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

这是我当前的操作表方法:

- (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 设计,所以请给我一个很酷的解决方案:))

谢谢!

【问题讨论】:

    标签: ios objective-c uiactionsheet uiactionsheetdelegate


    【解决方案1】:

    也许这会有所帮助:

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

    【讨论】:

      【解决方案2】:

      很有可能,很容易实现:

      这也适用于新的 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,应用确实会通过这种方式获得批准,但您确实会冒风险

      【讨论】:

      • 谢谢哥们,兄弟,我没有真正得到你的例子,我试过了,但我遇到了一些问题。刚刚编辑了我的问题以显示我的动作拍摄是如何工作的,您能否根据我的代码更改您的示例,也许我会得到它?非常感谢:)
      • UIActionSheet 已弃用 @nickshmick 您是否严格为 iOS8 编码?
      • @nickshmick 如果您没有为 >= iOS8 编码,请查看我修改后的答案
      猜你喜欢
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2015-12-07
      • 1970-01-01
      相关资源
      最近更新 更多