Search code examples
swiftuikit

Cannot not see a UIView subview of a UIView


I can not see a UIView subview that I added to my UIView. I have can clearly see it in the view heirarchy but I just cannot see it when the app is running.

Code:

var magnifyingView: UIView = {
    let view = UIView()
    view.alpha = 1.0
    view.backgroundColor = UIColor.yellow
    return view
}()

guard let cgImage = capturedImage else{return}
    let image = UIImage.init(cgImage: cgImage, scale: 1.0, orientation: UIImage.Orientation.right)
    imageView.image = image
    self.addSubview(imageView)
    bottomSection.addSubview(homeButton)
    bottomSection.addSubview(retakeButton)
    bottomSection.addSubview(continueButton)
    self.addSubview(bottomSection)

    showMagifyingView()
    self.bringSubviewToFront(magnifyingView)

private func showMagifyingView(){
    magnifyingView = UIView()
    magnifyingView.frame = CGRect.init(x: 30, y: 30, width: 200, height: 200)
    self.addSubview(magnifyingView)

    magnifyingView.translatesAutoresizingMaskIntoConstraints = false
    magnifyingView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.3).isActive = true
    magnifyingView.heightAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.3).isActive = true
    magnifyingView.topAnchor.constraint(equalTo: self.topAnchor, constant: 30).isActive = true
    magnifyingView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -30).isActive = true

}

View Heirarchy (The blue highlighted rectangle UIview is what I cannot see on my app at run time. Even though it is there and that I have brought it to the front)

enter image description here


Solution

  • You initialize magnifyingView at the top of your class

    var magnifyingView: UIView = {
        let view = UIView()
        view.alpha = 1.0
        view.backgroundColor = UIColor.yellow
        return view
    }()
    

    but you are adding a different magnifyingView = UIView() as subview in your showMagifyingView().