无法看到以编程方式添加到 UITabBar 的控制器 swift

Unable to see controller added to UITabBar programmatically swift

我从情节提要中添加了 2 个 tabBar 项,并以编程方式添加了一个 UITabBarItem - Menu。我成功地打开了与我使用故事板创建的 tabBarItems 对应的控制器。但是,当我点击 "Menu" 时,会出现一个空白的黑屏,

@objc public class MainScreenTabsController : UITabBarController {
public override func viewDidLoad() {
    super.viewDidLoad()
    let tabController = MyViewController()
    let tabBarItem = UITabBarItem(title: "Menu", image: UIImage(named: "more-options.png"), selectedImage: UIImage(named: "more-options"))
    tabController.tabBarItem = tabBarItem
    var array = self.viewControllers
    array?.append(tabController)
    self.viewControllers = array
}

public func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    return true;
}

public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
}
}

我遵循了几个添加标签栏项目的教程,但它们都有我编写的代码。我错过了一些非常基本的东西吗?

编辑:

Class 用于菜单控制器

@objc public class MyViewController:UIViewController {

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

public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
}
}

您的应用正在执行您的代码告诉它执行的操作。您正在创建 MyViewController 的实例并将其添加到 UITabBarController 的视图控制器数组中。

您的 MyViewController class 文件只是定义了一个空白的黑色视图。

我猜你在故事板中创建了一个 ViewController,你想用作 MyViewController?如果是这样,您需要从情节提要中对其进行实例化。

编辑故事板时,将 MyViewController class 分配给要使用的 VC,然后 给出它是 Storyboard ID - 例如 MyVC。然后,将 viewDidLoad 函数编辑为:

public override func viewDidLoad() {
    super.viewDidLoad()

    // wrong way
    //  let tabController = MyViewController()

    if let tabController = storyboard?.instantiateViewController(withIdentifier: "MyVC") as? MyViewController {

        let tabBarItem = UITabBarItem(title: "Menu", image: UIImage(named: "more-options.png"), selectedImage: UIImage(named: "more-options"))
        tabController.tabBarItem = tabBarItem
        var array = self.viewControllers
        array?.append(tabController)
        self.viewControllers = array

    }

}

因为您是以编程方式创建 ViewController 即。如果没有 nib/storyboard,您负责实例化一个 UIView 对象并设置视图控制器的视图 属性。为此,请实施 loadView 方法并将视图对象分配给 viewController 的视图 属性。然后你可以将自定义视图添加到视图对象,检查下面的代码。

class MyViewController: UIViewController {

    override func loadView() {
        // super.loadView()   // DO NOT CALL SUPER

        //create view
        view = UIView()
        view.backgroundColor = UIColor.white

        //Add a custom view with red color
        let customView = UIView()
        customView.translatesAutoresizingMaskIntoConstraints = false
        customView.backgroundColor = UIColor.red
        view.addSubview(customView)

        NSLayoutConstraint.activate(
            [customView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
             customView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
             customView.topAnchor.constraint(equalTo: view.topAnchor),
             customView.bottomAnchor.constraint(equalTo: view.bottomAnchor)]
        )
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
}

为此目的使用 Storyboard/Nib 会很好,因为您可以使用界面生成器中的自动布局轻松配置自定义 views/controls,而不是通过编程方式进行。 :)

编辑:

如果你正在使用故事板,那么实例化视图控制器就像下面的代码

class MainScreenTabsController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let tabController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MainViewController") as! MainViewController //if using storyboard 
        let icon = UITabBarItem(title: "Menu", UIImage(named: "more-options.png"), selectedImage: UIImage(named: "more-options")))
        tabController.tabBarItem = icon
        var controllers = self.viewControllers
        controllers?.append(tabController)
        self.setViewControllers(controllers!, animated: true)
    }
}