ios - 如何在 swift 5 中显示模态屏幕?

标签 ios swift screen modalviewcontroller

我目前正在从事 iOS 项目。

我的问题是,当我看到新屏幕时,我看不到旧屏幕,只看到新屏幕。

我想要一个模态屏幕。我错过了什么?

移动 ModalScreen

    func callAlert(_ text: String!) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let myAlert = storyboard.instantiateViewController(withIdentifier: "ModalViewController") as! ModalViewController
        myAlert.modalPresentationStyle = .overCurrentContext
        myAlert.modalTransitionStyle = .crossDissolve
        myAlert.text = text
        self.present(myAlert, animated: false, completion: nil)
    }

modalScreenController

class ModalViewController : UIViewController {

    var text: String?


    @IBOutlet weak var modalCustomAlert: UITextView!
    @IBOutlet weak var okButton: UIButton!

    @IBAction func okPress(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        modalCustomAlert.layer.cornerRadius = 8
        self.modalCustomAlert.text = text
    }
...

模态 Storyboard

imagestoryboard

最佳答案

您需要在 View Controller 的 View 中使用 alpha 组件设置背景颜色。

class ModalViewController : UIViewController {

    var text: String?


    @IBOutlet weak var modalCustomAlert: UITextView!
    @IBOutlet weak var okButton: UIButton!

    @IBAction func okPress(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5) //Your desired color and alpha component
        modalCustomAlert.layer.cornerRadius = 8
        self.modalCustomAlert.text = text
    }

关于ios - 如何在 swift 5 中显示模态屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58026299/

相关文章:

swift - SpriteKit SKAction 暂停延迟

C# - 减小图像大小的最佳方法是什么(用于通过 tcp 进行流式传输)

iphone - 为什么我的 Application Delegate 中的类变量没有被实例化?

ios - 为什么 UICollectionView 中的单元格被截断了?

ios - "Binary operator ' ~= ' cannot be applied to operands of type '

swift - 使用类的 init() 中声明的变量 - Swift 4

ios - 表达式类型不明确,没有更多上下文 swift 4.0

ios - 缩小 UINavigationBar 大标题文本的大小

android - 是否所有 10 英寸安卓平板电脑都被识别为 xlarge?

python - pygame 中的窗口在等待时卡住