Transparent background for modally presented viewcontroller

自作多情 提交于 2019-11-27 03:55:15
P5ycH0

Fixed it.

The problem was that presentViewController does not keep the view that I was covering.

viewController.modalPresentationStyle = .overCurrentContext

did the trick.

Nitin Nain

Part of the solution is hidden in the question. You need three lines to make the background transparent, viz. isOpaque = false backgroundColor = .clear & set the modalPresentationStyle

Here's the full solution. In the calling View Controller, call this function:

func presentModal() {
    let modalController = ModalViewController()
    modalViewController.modalPresentationStyle = .overCurrentContext
    present(modalViewController, animated: true, completion: nil)
}

And in ModalViewController's viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()

    view.isOpaque = false
    view.backgroundColor = .clear // try other colors, say: .white or black with Alpha etc.
}

same as the selected answer but visually through IB:

In case anyone is still struggling with a transparent background I found this solution some time ago - I can't remember where but still works fine with the latest Xcode & Swift.

ContactListViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource,UIViewControllerTransitioningDelegate {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    self.modalPresentationStyle = .custom
    self.transitioningDelegate = self
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!