//
//  DayControlsNavigatoinBar.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 12.03.2021.
//

import UIKit

class DayControlsNavigationBar: UINavigationBar {
    private let customView = UIView()
    private var contentView:UIView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        customView.backgroundColor = .red
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let contentView = self.contentView else {
            self.findAndSetupContentView()
            return
        }
        
        contentView.frame.origin.y = -30
        customView.frame = .init(x: contentView.frame.origin.x,
                                 y: contentView.bounds.height,
                                 width: contentView.bounds.width,
                                 height: contentView.bounds.height)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func findAndSetupContentView() {
        subviews.forEach {
            if String(cString: class_getName($0.classForCoder)) == "_UINavigationBarContentView" {
                self.contentView = $0
                self.contentView?.clipsToBounds = true
                self.contentView?.addSubview(customView)
            }
        }
    }
}
