swift4.0中,系统自带底部弹窗代码
时间: 2024-04-08 11:33:14 浏览: 13
在Swift 4.0中,系统没有提供直接的底部弹窗控件。但是你可以使用UIAlertController和UIAlertAction来创建一个自定义的底部弹窗。下面是一个示例代码:
```swift
func showBottomAlert() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in
// 处理点击 Action 1 的操作
}
alertController.addAction(action1)
let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in
// 处理点击 Action 2 的操作
}
alertController.addAction(action2)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// 处理点击取消按钮的操作
}
alertController.addAction(cancelAction)
if let popoverPresentationController = alertController.popoverPresentationController {
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.maxY, width: 0, height: 0)
popoverPresentationController.permittedArrowDirections = []
}
present(alertController, animated: true, completion: nil)
}
```
在上面的代码中,我们创建了一个UIAlertController,并添加了两个UIAlertAction作为选项,以及一个UIAlertAction作为取消按钮。然后,我们通过present方法将底部弹窗显示在屏幕上。
注意,为了将弹窗显示在底部,我们设置了popoverPresentationController的sourceView为当前的view,并且设置了sourceRect的位置在屏幕底部中间。这样就能够实现底部弹窗的效果。
希望这可以帮助你实现底部弹窗!如果还有其他问题,请随时提问。