Transparent Child View Controller's View - ios

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.

Related

swift - How to make size height view in navigation controller?

I want to ignore navigation bar and make full size view in navigation controller.
But the view is shown under the navigation bar.
Could I overlap navigation bar with black view?
I want to make like this picture
To achieve the given UI you should add the top view on window.To do so, First make an xib of top view. then add given code:
let frame = UIApplication.shared.keyWindow?.frame
let wrapper = UIView(frame: frame!)
wrapper.backgroundColor = UIColor.black.withAlphaComponent(0.35)
let objView = YourView() // Create your view object here.
objView.frame = wrapper.frame
objView.center = wrapper.center
wrapper.addSubview(objView)
UIApplication.shared.keyWindow?.addSubview(wrapper)
Set frame of YourView according to your requirement. wrapper makes your view transparent. You can make single view without wrapper. Use same code to add your view on window.

Changing width of child view controller

I added child view controller to parent view controller programmatically in swift 3.0.
But I do not want the child view controller width as full screen, I want to customise the width and height of the child view controller. I tried to open the custom size child view controller, but it is not working.
// Here is my code
let secondViewController = storyboard.instantiateViewController(withIdentifier: storyBoardName)
secondViewController.modalPresentationStyle = UIModalPresentationStyle.custom
secondViewController.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width-500, height: self.view.bounds.height)
self.present(secondViewController, animated: false, completion: nil)
Is there a way to achieve this?
In your code, you are not adding the secondViewController as childview controller. You are presenting that view controller. There is a difference.
You are trying to use UIModalPresentationStyle.custom to present viewcontroller using custom style. But using this custom style is not this trivial.
From documentation:
Creating a custom style involves
subclassing UIPresentationController and using its methods to animate
any custom view onto the screen and to set the size and position of
the presented view controller. The presentation controller also
handles any adaptations that occur because of changes to the presented
view controller’s traits
It tells you that you need to subclass UIPresentationController and subclass its method(with other methods) - (CGRect)frameOfPresentedViewInContainerView to set the frame of the presented viewcontroller.
More is explained in this link.
You can ofcourse achieve all this by actually adding a childviewcontroller.
Steps would be like this:
Create your child viewcontroller instance.
Set its view frame to whatever you want.
Add its view as a subview on to parent view controller's view using addSubview:
Call [addChildViewController] on parent viewcontrller (https://developer.apple.com/documentation/uikit/uiviewcontroller/1621394-addchildviewcontroller)
It depends on what you're trying to do, when I want to show some UI on top of another UIViewController I usually use a fullscreen view controller with self.modalPresentationStyle = .overFullScreen. Then I create another view which has the visible size I actually want to show to the user. This allows you to do pretty much anything you want.
But if you want an actual child viewcontroller, you need to use the appropriate functions for it.

How to set a view over a child view controller's view?

I am wondering if it is possible to set a view controller's view over one of its child view controller's view ?
Let's explain that with an example :
Let's say I have two UIViewControllers : parentVCand childVC. childVC is a child of parentVC.
Now, I have a UIButton ( called button ) which is a subview of parentVC.view.
I set childVC from parentVC like that :
self.addChildViewController(childVC)
childVC.didMoveToParentViewController(self)
childVC.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height
self.view.addSubview(childVC.view)
Now, I want button to show up even if childVC.view covers parentVC.view.
Do you guys think there's a way to do that ? I tried to play with button.layer.zPosition but I couldn't succeed.
Yes, you can, you just need to add the button to the view hierarchy or move the button up in the hierarchy after you add the child view controller.
yes you can do that first of all when you load a childview on a viewcontroller make a view like. There is a container on top covering almost 90% screen and at bottom there is a button,So when you load the view on container and sub view show's you on top if you tap a button then add a subview and bring that subview to front.

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()
}

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