InsertSubview getting hidden behind another subview

I have an app that has the instagram-stories or snapchat stories feature . I display an image and on top of that image there is information like name and content . When I tap the image it is suppose to display another image but it doesn't only when using InsertSubview. Everything updates correctly except the image that I use as a subview . When i use AddSubview instead of InsertSubview then on tap it changes images however none of the content on top of the image would show then . What I think that is happening is that the new image is getting buried behind the old image since on every tap a new InsertSubview is created . Is there a way that I can fix this ? Again when using InsertSubview it gets stuck on the first image and does not change images on tap. I verified this by uploading a video it showed an image first and when I tapped the image I could here a video in the background eventhough it showed that first image .This is my code



On Touch


   override func touchesBegan(_ touches: Set, with event: UIEvent?) {

        for touch in touches {
            let location = touch.location(in: self.view)
      
             else if location.x <  self.view.layer.frame.size.width / 2 {
                if touchCount == 0 || touchCount < 0 {
                    touchCount = 0
                } else {
                    touchCount = touchCount - 1
                   
                }
               reloadTable(Offset: touchCount)
            }
            else {
                touchCount = touchCount + 1
                reloadTable(Offset: touchCount)

            }
        }
    }


Uploads Image


func ShowImage(s_image: String) {

       
        self.bookviewImage.imageView = nil
        let imgURL: NSURL = NSURL(string: s_image)!
        let request:NSURLRequest =  NSURLRequest(url: imgURL as URL)
        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)
       
        bookviewImage.imageView = UIImageView(image: UIImage(named: s_image))
        bookviewImage.imageView?.frame = VideoView.bounds
        bookviewImage.imageView?.contentMode = UIViewContentMode.scaleAspectFill
      
       
        let task = session.dataTask(with: request as URLRequest,    completionHandler: {(data, response, error) in
            DispatchQueue.main.async(execute: { () -> Void in
               
                if data != nil {
                    self.bookviewImage.imageView?.image = UIImage(data: data!)
                    self.VideoView.insertSubview(self.bookviewImage.imageView!, at: 0)
                    self.addChildViewController(self.bookviewImage)
                   // self.VideoView.addSubview(self.bookviewImage.imageView!)
                   
                } else {
                    //    cell.Stream_Image_Height.constant = 0
                    self.bookviewImage.imageView!.image = nil
                }
               
            })
        });
        task.resume()
    }



Http Request


func reloadTable(Offset: Int) {
// basic sends http request the offset is the touchCount
// I call the method below when the http data comes 
// again this part works fine
ShowImage(s_image: s_image)
}

addSubView does an insertSubView on top (with an index equal to the number of existing subviews). So it will hide all other subviews.


insertSubView (at: 0) inserts subviews behind all. That may not be what you want.


So you should either:

- know at which z-level you want to insert

- or keeping an explicit reference of the subview, use insertSubview(_:aboveSubview:) or insertSubview(_:belowSubview:)

Accepted Answer

This is not a question about a feature of the Swift language. So it’s posted in the wrong forum. Cocoa Touch or Getting Started might be better.


As Claude says, index 0 is at the back, behind all other views. So it’s doing what you ask. You can use Xcode‘s view debugger to inspect the view hierarchy at runtime to see how it all works.


Why are you creating a new UIImageView anyway? Why not just replace the UIImage in a single existing UIImageView created on a storyboard or in viewDidLoad? That way you can set up your view hierarchy once, and never have to worry about it again.

I am new to swift and did not think of that however creating the Image in ViewDidLoad worked, thanks .

SO, what did you change in your code ?

InsertSubview getting hidden behind another subview
 
 
Q