如何用代码添加UIView?不使用故事板

how to add UIView with code? Without using storyboard

我是初学者,只能通过故事板添加元素。

现在我想知道如何在不使用故事板的情况下将 UIView 代码添加到屏幕上。

我需要将它放在屏幕的最顶部,全宽,用 UIView 替换 NavigationBar

import UIKit

class ChatsNavBar: UIView {

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var imageUser: UIImageView!
    @IBOutlet weak var backButton: UIButton! {
        didSet {
            backButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
    }
    @IBOutlet weak var navView: UIView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    
    private func commonInit() {
        let bundle = Bundle(for: ChatsNavBar.self)
        bundle.loadNibNamed("ChatsNavBar", owner: self, options: nil)
        navView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(nameLabel)
        addSubview(navView)
        addSubview(statusLabel)
        addSubview(imageUser)
        addSubview(backButton)
        nameLabel.frame = self.bounds
        nameLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }
    
    @IBAction func backAction(_ sender: Any) {
    }
}

class ChatsViewController: MessagesViewController {
    
    init(user: MUser, chat: MChat) {
        self.user = user
        self.chat = chat
        super.init(nibName: nil, bundle: nil)

        title = "\(chat.friendUsername) \(chat.friendUserSurname)"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configureMessageInputBar()
        
        let navController = UIView()

这是将其放置在整个屏幕上的方法

   let viewOverLay = ChatsNavBar()
    navigationController.view.addSubview(viewOverLay)
    viewOverLay.frame = navigationController.navigationBar.bounds