我试图使行动表,因为它显示在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)
6条答案
按热度按时间n8ghc7c11#
UIAlertController扩展了UIViewController,UIViewController有一个视图属性。你可以根据自己的意愿向该视图添加子视图。唯一的问题是如何正确地调整警报控制器的大小。你可以这样做,但这可能会在苹果下次调整UIAlertController的设计时很容易地中断。
雨燕3
雨燕
目标-C
话虽如此,一个不那么笨拙的方法是创建自己的视图子类,其工作原理类似于UIAlertController的UIAlertActionStyle布局。事实上,相同的代码在iOS 8和iOS 9中看起来略有不同。
iOS 8系统x1c 0d1x
iOS 9
iOS 10系统
pinkon5k2#
到目前为止,我使用自动版式约束找到的最简洁的解决方案:
输出:
lymnna713#
我为UIAlertController编写了一个扩展(在Swift 4中),它解决了自动布局的布局问题,甚至还有一个后备消息字符串,以防某些东西不起作用(由于UIAlertController布局的未来变化)。
下面是一个用法示例:
它会显示如下内容:
rjzwgtxy4#
对于懒惰的人来说,Swift 3.0和iOS〉= 9优化版本的@Keller答案是:
csga3l585#
我尝试过解决
UIAlertController
的局限性,但是不管我怎么做,它都不够好。如果你还在纠结这个问题,我已经创建了a library,它可能会有所帮助。它允许你用一堆内置类型创建自定义工作表。它也可以扩展和重新设计样式。wribegjk6#
下面是@Cesare解决方案的Objective-C版本