Adrian
Adrian

Reputation: 20078

How to bring a subview's subview to top?

The following print screen shows my layout:

enter image description here

I have the main view , then a Top view and then a picture.

When I press on the picture, I want a semi transaprent black screen appearing on the entire view:

        let scrennRect = UIScreen.mainScreen().bounds
        let coverView = UIView(frame: scrennRect)
        coverView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
        self.view.addSubview(coverView)

But I want my picture to stay on top of the coverView ?
Is there a way I can bring only the picture to be in front of the coverView ?

Upvotes: 1

Views: 737

Answers (2)

Rough Pilot
Rough Pilot

Reputation: 114

The problem is that your coverView and imageView have different super views.

That's why you cannot bring the imageView to the front.

In order to bring the imageView to the front you need to move it outside of the Top View so your View contains the Top view and the imageView.
Then, you can add the coverView to the main view, and bring the imageView to the front.

Another solution: Place an image view inside the coverView at the exact same position.

Upvotes: 3

Rohit Kumar
Rohit Kumar

Reputation: 887

This should do your work. Any view maintains a stack of its subviews.

self.view.addSubView(blackTransparentView);
self.view.bringSubViewToFront(myImageView);

Upvotes: 0

Related Questions