Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – UIAlertController – 将自定义视图添加到操作表大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

当我们尝试附加一个图像在屏幕截图中时,我正在尝试制作在iOS上的消息应用程序中显示的操作。 我在新的UIAlertController中意识到,我们无法适应任何自定义视图。我可以做什么这样做吗? 我的代码看起来很标准 let alertController = UIAlertController(title: "My AlertController", message: "tryna show s
@H_801_10@
当我们尝试附加一个图像在屏幕截图中时,我正在尝试制作在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,它具有一个view属性。您可以将该视图的子视图添加到您心中的愿望。唯一的麻烦是正确地调整警报控制器的大小。你可以做这样的事情,但是这可能会在下次苹果调整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,completion:{})
    }

迅速

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

let margin:CGFloat = 10.0
let rect = CGRect(x: margin,height: 120)
let customView = UIView(frame: rect)

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

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

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

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

self.present(alertController,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 – UIAlertController – 将自定义视图添加到操作表全部内容,希望文章能够帮你解决swift – UIAlertController – 将自定义视图添加到操作表所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。