简体   繁体   中英

How to bring a subview's subview to top?

The following print screen shows my layout:

在此处输入图片说明

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 ?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM