UIAlertController-将自定义视图添加到操作表

CalZone

当我们尝试附加屏幕截图中的图像时,我正在尝试制作iOS上的消息应用程序中显示的操作表。

我意识到在新的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)

在此处输入图片说明

凯勒

UIAlertController扩展了具有视图属性的UIViewController。您可以向该视图添加子视图,以实现您的内心需求。唯一的麻烦是正确调整警报控制器的大小。您可以执行类似的操作,但是下次Apple调整UIAlertController的设计时,这很容易中断。

迅捷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:{})
    }

迅速

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:{})

目标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 在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用UIAlertController按钮将自定义单元格添加到tableView

UIAlertController中的自定义视图

将自定义视图添加到警报视图

无法将警报操作添加到UIAlertController(Swift)

将图像添加到UIAlertController

将自定义按钮操作添加到笔管

如何定位UIAlertController的视图?

将自定义标题视图添加到表格视图

如何在Swift中将TextField添加到UIAlertController

是否可以将超链接添加到UIAlertController?

如何将 UIDatePicker 添加到 UIAlertController 的 textField 中?

呈现UIAlertController时,UIBarButtonItem丢失自定义外观属性

Android-将自定义视图添加到xml布局中的组件

如何将自定义xtype添加到另一个视图?

以编程方式将自定义按钮添加到具有约束的视图

将自定义视图添加到 NavigationBar 中不起作用?

如何将自定义视图添加到Django管理界面?

将自定义视图类添加到屏幕管理器

将自定义属性添加到现有视图

如何将自定义视图组添加到Anko DSL?

如何正确地将自定义视图添加到工具栏?

将自定义视图添加到工具栏

以编程方式将自定义视图添加到片段布局

将自定义标题添加到集合视图中

如何将自定义视图页面添加到 django 管理员?

将自定义标签添加到拆分视图中的原型单元格

将自定义字段添加到现有视图的问题

将自定义行为属性添加到视图

使用 UIApplication.shared.keyWindow?.addSubview Swift 将自定义视图添加到屏幕