Transparent background for modally presented viewcontroller
Asked Answered
R

5

77

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.

But.... Once the animate-in of the PFLoginViewController is done, the background turns black... Whilst during the animation the background was transparent.

func presentLogin() {
    var loginViewController = LoginViewController()
    var signupViewController = SignUpViewController()

    loginViewController.fields = .UsernameAndPassword | .LogInButton | .PasswordForgotten | .SignUpButton | PFLogInFields.DismissButton
    signupViewController.fields = .UsernameAndPassword | .Email | .DismissButton | .SignUpButton
    loginViewController.delegate = self
    loginViewController.logInView.backgroundColor = UIColor.clearColor()
    loginViewController.logInView.opaque = false
    signupViewController.delegate = self

    loginViewController.signUpController = signupViewController

    self.presentViewController(loginViewController, animated: true) { () -> Void in
        //
    }
}

My logincontroller's used subclass:

class LoginViewController: PFLogInViewController {

    @IBOutlet weak var _nmcLogoLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let userName = SettingsManager.userName
        let password = SettingsManager.password

        self.logInView.usernameField.text = userName
        self.logInView.passwordField.text = password

        NSBundle.mainBundle().loadNibNamed("LoginViewBranding", owner: self, options: nil)[0] as? UIView

        self.logInView.logo = _nmcLogoLabel

    }

}

How can I make it transparent?

P.s. Applying the clearColor to the backgroundColor in the subclass makes no difference

Ragin answered 27/12, 2014 at 17:30 Comment(0)
R
172

Fixed it.

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

viewController.modalPresentationStyle = .overCurrentContext

did the trick.

Ragin answered 27/12, 2014 at 18:7 Comment(6)
can you also comment the code for "pushViewController" .Sussna
Working solution !!Junkman
this is the secret!Weatherglass
But that will not add the presented controller to the responder chainShagbark
If use modalPresentationStyle = .overCurrentContext, after dismiss view controller, viewWillAppear of parent viewController not being called. Is any way to fix this?Analogize
This helped me while working with UIKit and SwiftUIFranciscafranciscan
R
56

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.
}
Rittenhouse answered 1/6, 2017 at 11:14 Comment(2)
this worked for me when I set the code in the Init, not in the viewDidLoad methodTiptop
@NitinNain, I set view.alpha = 0.35 but all view controller's control also set with alpha and look blurBlent
G
23

same as the selected answer but visually through IB:

enter image description here

Girder answered 4/12, 2016 at 3:3 Comment(1)
You need also to set up the presented controller as Nitin Nain answerRudelson
P
7

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
   }
Pannonia answered 13/7, 2020 at 9:0 Comment(1)
This is great view.backgroundColor = .clearFranciscafranciscan
T
0

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
}
Tour answered 11/7, 2019 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.