Search code examples
iosxamarinuiviewstoryboarduicontainerview

UIView on top of a Container View


Is it possible to display an UIView on top of a container View?

I want to add a view with a few opacity background to still see my container View. But everything i tried made either my containerView disappear completely or on top of my View. I tried via Storyboard and code.

I'm sure I'm missing something.


Solution

  • just add your view to the view property of your container controller's container

    simple:

    let viewYouWantToAddSubviewTo = parent?.view
    

    detail:

    import UIKit
    
    class CustomNavigationViewController: UINavigationController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupViews()
        }
    
        func setupViews() {
            let layout = UICollectionViewFlowLayout()
            let rootVC = HomeCollectionViewController(collectionViewLayout: layout)
            viewControllers = [rootVC]
            let v = UIView()
            v.backgroundColor = UIColor.blue
            v.layer.opacity = 0.4
            v.translatesAutoresizingMaskIntoConstraints = false
    
            // add your view to this view of the controller's container
            let vv = (parent?.view)! 
    
            vv.addSubview(v)
    
            // constraints for v
            v.leftAnchor.constraint(equalTo: vv.leftAnchor).isActive = true
            v.rightAnchor.constraint(equalTo: vv.rightAnchor).isActive = true
            v.topAnchor.constraint(equalTo: vv.topAnchor).isActive = true
            v.bottomAnchor.constraint(equalTo: vv.bottomAnchor).isActive = true
        }
    }
    

    result:

    enter image description here