0

我正在尝试将导航控制器作为子视图添加到容器视图中。容器视图对我来说是一种新事物,但我想我现在理解它们了......我成功地构建了一个带有容器视图的演示项目......但是一旦我尝试添加一个 ViewController(VC),它位于导航堆栈,这个 VC 只是不可见,我只是找不到解释:?

我确实想在代码中而不是在情节提要中添加这些孩子,因为我将在现有项目中使用此功能,并且我想让它尽可能简单,有人可以帮助我吗?

故事板截图

主视图控制器:

import UIKit

class MainViewController: UIViewController {
    
    let naviVC = ViewController()
    let menuVC = SideMenuViewController()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .green
        
        addChild(naviVC)
        addChild(menuVC)
        
        self.view.addSubview(naviVC.view)
        self.view.addSubview(menuVC.view)
        naviVC.didMove(toParent: self)
        menuVC.didMove(toParent: self)
        
        naviVC.view.frame = self.view.bounds
        menuVC.view.frame = self.view.bounds
        
        naviVC.view.isHidden = false
        menuVC.view.isHidden = true
        
    }
}

ViewController(在导航内):

import UIKit


class ViewController: UIViewController {
    
    let menuVC = SideMenuViewController()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        configureNaviBar()

    }
    
    
    //MARK: Navi Bar
    func configureNaviBar() {
        // Left Bar Button Item
        let burgerButton = UIImage(systemName: "line.horizontal.3")
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: burgerButton, style: .plain, target: self, action: #selector(showMenu))
    }
    
    @objc func showMenu() {
        print("Button pressed")
                // do something
        menuVC.view.isHidden = false
        print("menuVC is hidden = \(menuVC.view.isHidden)")


    }
}

SideMenuViewController:

class SideMenuViewController: UIViewController {
    

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
4

0 回答 0