UIView background color changing when view controller finishes loading - ios

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.
Is there anyway to maintain the alpha component after if finishes loading?

Step one: Change this to an overFullScreen presentation.
Step two: There is no step two.

just set presentaion style on viewContriller
[myViewcontroller setModalPresentationStyle:UIModalPresentationCustom];
[myViewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self.navigationController presentViewController:myViewcontroller animated:true completion:nil];

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.
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.
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.

Related

Transparent Child View Controller's View

I am creating a child view controller in a parent view controller and presenting it with the code below -
self.addChildViewController(childVC)
childVC.view.frame = self.view.bounds
self.view.addSubview(childVC.view)
childVC.view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
childVC.didMove(toParentViewController: baseVC)
The child view controller simply displays a 200 X 300 image view right in the middle. I want the child view controller to blur the parent view controller's view and display this opaque image view. However I can't seem to get it to show the underlying parent view controller's contents no matter what I do. I already tried the following in child view controller's viewDidLoad -
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
What am I missing here? Is there a better approach? The child view controller's purpose is to encapsulate the image preview logic by blurring the parent view controller's contents and display a UIImageView right in its view's center.
Try using ChildVC.backgroundColor = UIColor.black.withAlphaComponent(0.5). This code makes the child view appear semi transparent.
Hope this helps.
Create a new ViewController as a ChildViewController.
Give segue to ChildViewController from ParentViewController.
set following property
You have to select Presentation as Over current context
You can also change the Transition based on your requirement
See following image
Set background color of childViewController view as what you want and set its opacity 50% or 60% , whatever you want.
This will show parent view with blur effect.
My original solution -
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
is working. My parent view controller's view is white with very sparse detail so I assumed that the parent view isn't visible, silly overlook on my part.

Transparent modal view on top of another view

I'm trying to create a modal view using Xcode's storyboard. I want it to consist of a small opaque view within a transparent view, so as to look like what would be called a popup window in desktop terminology.
I've created a segue to the new view controller, set it to "Present Modally" and its presentation to "Current Context", created my opaque view in the new view controller, and then set the background of the view of the new view controller to "Clear Color", and unchecked "Opaque".
When I trigger the segue, the new box slides into place over the previous view, exactly as it should, but then as soon as the animation ends, the background goes black.
What's going wrong here? How can I make the background stay transparent and the old view remain visible?
You can use this category and change the code for you have the transparent modal view. In file UIViewController+ENPopUp.m you just have add [popUpView setBackgroundColor:[UIColor clearColor]]; in the end of presentPopUpView method.
I hope this can help you.
You need to use View Controller Containment:
iOS Developer Library
// Sample code you can use in your UIViewController
func addChildVC(child: UIViewController) {
self.addChildViewController(child)
child.view.bounds = self.view.bounds
self.view.addSubview(child.view)
child.didMoveToParentViewController(self)
}
func removeChildVC(child:UIViewController) {
child.willMoveToParentViewController(nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}

Grey color background on pushing transparent background UIViewController inside a Container View (Storyboard)

I'm pushing a UIViewController (A) with clearColor as background in a UINavigationController (N).
N is placed inside a Container View. This view has a background that I want to show always.
Then, when Push is animated it shows a grey/transparent background in my VC (A) and when it finishes its animation, it shows correctly.
Is there any way to avoid this grey color?
I have 'done' it by putting a white view background in A and making it disappear in viewDidAppear but I think it can be done without that trick...
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
__weak TestsViewController *weakSelf = self;
[UIView animateWithDuration:.3 animations:^{
TestsViewController *ownSelf = weakSelf;
ownSelf.backgroundView.alpha = 0;
}];
}
Set your app's window's background colour to white:
// applicationDidFinishLaunching
self.window.backgroundColor = [UIColor whiteColor];
The push animation often ends up using the background colour of the window behind the transparent areas, and the window background colour is clear by default, so it shows black through the transparent navigation bars and toolbars.
EDIT: Sorry, misunderstood what the issue was. When you push a view controller with a transparent background colour, the background you see behind the view controller is the shadow that is rendered behind your view controller (you can see the edge of it when in a non-transparent view controller).
This question has an answer to your issue. Basically, you have to implement a custom animation controller, to provide the transition animation yourself (as described here). It would probably be easier to continue using your trick than implementing your own custom navigation transition.

overlapping Views during Push transition

I have a storyboard in which i am pushing one view on top of another view in the navigation controller.(Simple push Segue). I am setting the background of the new view in the viewDidLoad method and using the following code to do so.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"welcome_bg_pattern"] ];
But during the push animation form one view to the other view i can see both the views overlapping. Can some body can guide me how i can set the background so that i don't have that problem.
Here are the attach imaged
first View
During push Transition. here you can see both the vies are overlapped this will not happen if i just simple set a redColor as background color of the view
Second View
I have also tried by setting the background image in the viewWillAppear
Any help will be great
Instead of set background image of self.view like
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"welcome_bg_pattern"] ];
set background image in UIImageView with set proper frame and add UIImageView as subView of self.view.

How can I add a transparent view?

I want to push a view controller onto the navigation stack but I don't want its view to initially appear - in other words I want the view that was visible when the view controller is push to still be visible.
I tried setting the view controller's view's alpha value to 0.0 which I thought would make it transparent. But instead what is happening is that when I push the view controller on the the stack the screen is white. If I set the alpha to 1.0 then the view controller's view appears as expected.
Why is it white and not transparent?
you will have to add the view to the viewcontrollers manually
Not pushing it
For example do the following
YourViewController *vc = [[YourViewController alloc] init];
[self.view addSubview:vc.view];
vc.view.alpha = 0.0;
//Animate Here
vc.view.alpha = 1.0;
//Commit Animate Here
Please not that you will have to do some additional coding to implement the release of the vc, since now you have retained vc.view you will not be able to release vc easily,
Another solution is instead of implementing the second view as a viewcontoller implement it as uiview, and the xib class will be view and not uiviewcontroller
Maybe make sure that the opaque property is set to NO?
Or perhaps the view you're pushing on was built in interface builder, and you have a background color of white with another view you put on top of it and you only changed the opacity of the subview?

Resources