Transparent background for modally presented viewcontroller

前端 未结 5 1343
再見小時候
再見小時候 2020-11-29 03:04

I am using Parse & ParseUI. I want my PFLoginViewController subclass to have a transparent background. In the future, I want to lay a blurred view over the background.

相关标签:
5条回答
  • 2020-11-29 03:39

    Fixed it.

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

    viewController.modalPresentationStyle = .overCurrentContext
    

    did the trick.

    0 讨论(0)
  • 2020-11-29 03:49

    I have two view controller 1st login & 2nd forgot password. Initially, the login screen has been visible on the tap of forgot password button, forgot password screen will be present over the login screen with a transparent background.

    In the Login screen, I have used following code for forgotButtonAction to present forgotPasswordViewController

       let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "forgotPasswordViewController") as! forgotPasswordViewController
        vc.modalPresentationStyle = .overCurrentContext
        present(vc, animated: true, completion: nil)
    

    And also in the forgotPasswordViewController, Under the ViewDidLoad method, I have to change the view background color and opaque state through the following code

       override func viewDidLoad() {
        super.viewDidLoad()
        view.isOpaque = false
        view.backgroundColor = .clear
       }
    
    0 讨论(0)
  • 2020-11-29 03:51

    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
    }
    
    0 讨论(0)
  • 2020-11-29 03:52

    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.
    }
    
    0 讨论(0)
  • 2020-11-29 03:55

    same as the selected answer but visually through IB:

    0 讨论(0)
提交回复
热议问题