Swift : 出现在屏幕底部的OverlayView

Swift : OverlayView that appears at the bottom of the screen

我想创建一个非常常见的效果,如下图所示:

说明:我想要实现的效果是在用户单击按钮时出现(滑入)屏幕底部的视图:您仍然可以看到该视图后面的屏幕,但它适用一个 "dark layer" (黑色视图,比如说 60% 的不透明度)在它的顶部。当用户点击 Block 或 Report abuse(如本例)时,它将执行相应的操作。现在,当用户点击取消时,以及当他点击 "dark layer" 上的任何地方时,他都会回到屏幕上。

我尝试了什么:展示一个新的视图控制器(但它会使用比必要更多的数据),使用覆盖层,但我什至没有接近我们通常在应用程序上看到的那种效果。我不确定,但我想说获得这种效果的最好方法是使用视图?

有人有想法吗?

谢谢,祝你有美好的一天,

J

您正在寻找名为 UIActionSheet 的本地元素。它已成为 UIAlertController 的一部分,并且完全符合您的要求。

这里有一些关于如何设置的帮助:

 // Create you actionsheet - preferredStyle: .actionSheet
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

        // Create your actions - take a look at different style attributes
        let reportAction = UIAlertAction(title: "Report abuse", style: .default) { (action) in
            // observe it in the buttons block, what button has been pressed
            print("didPress report abuse")
        }

        let blockAction = UIAlertAction(title: "Block", style: .destructive) { (action) in
            print("didPress block")
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
            print("didPress cancel")
        }

        // Add the actions to your actionSheet
        actionSheet.addAction(reportAction)
        actionSheet.addAction(blockAction)
        actionSheet.addAction(cancelAction)
        // Present the controller
        self.present(actionSheet, animated: true, completion: nil)

它应该产生以下输出: