UIView won't appear in simulator

144 views Asked by At

I would like ask you about the reason that why UIView(it has orange background color) won't appear.

https://i.stack.imgur.com/FoMlo.jpg This link has a picture showing UIView in preview. https://i.stack.imgur.com/onmrn.jpg The picture of this link shows UIView won't appear in simulator.

Below is ViewController code. I've already had that Z-axis of gradientLayer got a negative number.

extension UIColor {
    convenience init(hexString: String) {
        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int = UInt32()
        Scanner(string: hex).scanHexInt32(&int)
        let a, r, g, b: UInt32
        switch hex.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (255, 0, 0, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var playerView: UIView!
    @IBOutlet weak var controlView: UIView!
    @IBOutlet weak var controlPanel: UIView!
    @IBOutlet weak var progressView: UIView!

    // playerView에서 gradient background color
    var gradientLayer: CAGradientLayer!

    func createGradientLayer() {
        gradientLayer = CAGradientLayer()
        gradientLayer.frame = playerView.bounds
        gradientLayer.colors = [UIColor(hexString: "#385175").cgColor, UIColor(hexString: "#36a9ba").cgColor]
        gradientLayer.zPosition = -2
        playerView.layer.addSublayer(gradientLayer)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        controlPanel.layer.cornerRadius = 7.0
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        createGradientLayer()
    }
}

How can I make UIView appears? Thanks for reading.

1

There are 1 answers

4
2h4u On BEST ANSWER

Hard to tell whats going on without the full project but you can debug it yourself: enter image description here

At step 2. you need to select one of your views which are not displayed. At step 3. you can see the dimensions of the selected view, so you know if the height or width are set to 0 (which would cause to view to not be visible).

If the height and width are bigger than 0, than you then you can rotate the rendering of your app within xcode and see the different layers of your app. If the orange views are "under" some other views, you should see them there.

Post what you find out, so it is easier to help you :)