当我们试图像截图一样附上一张图片时,我正在try 制作iOS上的messages应用程序中显示的操作表.

我意识到在新的UIAlertController中,我们不能适应任何自定义视图.我能做到这一点吗?

我的代码看起来很标准.

    let alertController = UIAlertController(title: "My AlertController", message: "tryna show some images here man", preferredStyle: UIAlertControllerStyle.ActionSheet)

        let okAction = UIAlertAction(title: "oks", style: .Default) { (action: UIAlertAction) -> Void in
        alertController.dismissViewControllerAnimated(true, completion: nil)
    }
    let cancelAction = UIAlertAction(title: "Screw it!", style: .Cancel) { (action: UIAlertAction) -> Void in
        alertController.dismissViewControllerAnimated(true, completion: nil)
    }

    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    self.presentViewController(alertController, animated: true, completion: nil)

enter image description here

推荐答案

UIAlertController扩展了具有view属性的UIViewController.你可以将子视图添加到你内心渴望的视图中.唯一的问题是正确调整alert 控制器的大小.你可以这样做,但下一次苹果调整UIAlertController的设计时,这可能很容易被打破.

Swift 3

    let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)

    let margin:CGFloat = 10.0
    let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 120)
    let customView = UIView(frame: rect)

    customView.backgroundColor = .green
    alertController.view.addSubview(customView)

    let somethingAction = UIAlertAction(title: "Something", style: .default, handler: {(alert: UIAlertAction!) in print("something")})

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(alert: UIAlertAction!) in print("cancel")})

    alertController.addAction(somethingAction)
    alertController.addAction(cancelAction)

    DispatchQueue.main.async {
        self.present(alertController, animated: true, completion:{})
    }

Swift

let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)

let margin:CGFloat = 10.0
let rect = CGRect(x: margin, y: margin, width: alertController.view.bounds.size.width - margin * 4.0, height: 120)
let customView = UIView(frame: rect)

customView.backgroundColor = .green
alertController.view.addSubview(customView)

let somethingAction = UIAlertAction(title: "Something", style: .default, handler: {(alert: UIAlertAction!) in print("something")})

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {(alert: UIAlertAction!) in print("cancel")})

alertController.addAction(somethingAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion:{})

Objective-C

  UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

  CGFloat margin = 8.0F;
  UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(margin, margin, alertController.view.bounds.size.width - margin * 4.0F, 100.0F)];
  customView.backgroundColor = [UIColor greenColor];
  [alertController.view addSubview:customView];

  UIAlertAction *somethingAction = [UIAlertAction actionWithTitle:@"Something" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
  UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
  [alertController addAction:somethingAction];
  [alertController addAction:cancelAction];
  [self presentViewController:alertController animated:YES completion:^{}];

话虽如此,一种不那么黑客的方法是创建自己的视图子类,其工作方式类似于UIAlertController的UIAlertActionStyle布局.事实上,同样的代码在iOS 8和iOS 9中看起来略有不同.

iOS 8

iOS 9

iOS 10

Swift相关问答推荐

解析SON数据的API调用有什么问题?

为表单部分赋予背景 colored颜色 /渐变

在visionOS 1.0中已弃用';base Color';:请改用`Color`属性

在visionOS RealityView中使用.GenerateText时,未显示Reality Composer Promaterial 纹理

如何强制创建新的UIViewControllerRenatable|更新Make UIViewController上的视图

在不传递参数的情况下使用Init方法?

可选,类似于自定义类型的初始化

如何绑定环境变量ios17

如何在设备(或常量)地址空间中创建缓冲区?

为什么 XUIView 中的 Binding 看不到更新后的值?

如何将视图添加到多行文本视图的末尾?

swift 是否遇到 Java 的 2gb 最大序列化大小问题?

响应 UITextField (UIViewRepresentable) 中的特定按键

SwiftUI Preview 不适用于 Core Data

是否可以使任何协议符合 Codable?

将项目引用添加到 Swift iOS XCode 项目并调试

是否可以在 Swift 中创建通用闭包?

如何实现 Swift 的 Comparable 协议?

Swift 中 NSFetchRequest 的多个 NSPredicates?

Swift 3:日期与 NSDate?