0

I'm loading a view controller modally via another view controller and I'm trying to change the background color using:

 override func viewDidLoad() {
    super.viewDidLoad()

    transparentBG.backgroundColor? = UIColor.black.withAlphaComponent(0.4)

    // transparentBG is a UIView defined in storyboard
}

While the view is animating into position (sliding up) it maintains the alpha value I set. But once it reaches the top of the screen it removes the alpha component and is changing the color to what looks like the color with the alpha component, so like a gray color, but with no transparency as seen in the image below.

enter image description here

Is there anyway to maintain the alpha component after if finishes loading?

Tyler Rolfe
  • 194
  • 1
  • 9
  • Do you want to see the previous screen's view through the semi-transparent top-view controller ? Is that the purpose of adding this alpha to the subview ? – Tushar Oct 27 '16 at 14:27
  • 1
    The view underneath is removed when the modal view has finished animating. – James P Oct 27 '16 at 14:35
  • 1
    Possible duplicate of [Transparent Modal View on Navigation Controller](http://stackoverflow.com/questions/849458/transparent-modal-view-on-navigation-controller) – James P Oct 27 '16 at 14:43
  • Tushar - Yeah I want the existing view controller to be visible underneath it. – Tyler Rolfe Oct 27 '16 at 14:47
  • James P - Ah...that makes sense. Thanks for the link! – Tyler Rolfe Oct 27 '16 at 14:47

3 Answers3

1

Step one: Change this to an overFullScreen presentation.

Step two: There is no step two.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

just set presentaion style on viewContriller

[myViewcontroller setModalPresentationStyle:UIModalPresentationCustom];
[myViewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self.navigationController presentViewController:myViewcontroller animated:true completion:nil];
Hamid Zandi
  • 2,714
  • 24
  • 32
0

What is happening is that the alpha is being kept, but the previous view is being removed once the animation is complete.

There are a couple approaches you can take.

  1. Take a screen shot of the previous view and insert that as a background in the new view. Look at the drawViewHierarchyInRect function. You can grab the screen shot in the new view controller's init method, then set it as a background image in the viewDidLoad.

  2. The other approach would be to add the overlay as a subview, either to the existing view, or even the window itself.

I've used both methods successfully.

picciano
  • 22,341
  • 9
  • 69
  • 82