Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent background for modally presented viewcontroller

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

like image 583
Tycho Pandelaar Avatar asked Dec 27 '14 17:12

Tycho Pandelaar


People also ask

How do I present a transparent Viewcontroller?

When you modally present a view controller the , it goes on the stack which in turn hides the view controller below it. So all you can do is to present a transparent view with animation similar to modally presenting a view controller.

How do you make a Viewcontroller transparent in Swift?

Just add a Storyboard Segue with Kind set to Present Modally to your modal view controller and on this view controller set the following values: Background = Clear Color. Drawing = Uncheck the Opaque checkbox. Presentation = Over Current Context.


2 Answers

Fixed it.

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

viewController.modalPresentationStyle = .overCurrentContext 

did the trick.

like image 180
Tycho Pandelaar Avatar answered Sep 29 '22 10:09

Tycho Pandelaar


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. } 
like image 42
Nitin Nain Avatar answered Sep 29 '22 10:09

Nitin Nain