我正在做一个有趣的项目,我创建了一个选项卡栏,但不幸的是它推送了一个新视图,并且我试图在我的家庭控制器中实现它。有人可以解释或告诉我如何使选项卡栏出现在我的家庭控制器中。谢谢。

这是家庭控制器

import UIKit
import Firebase
import SideMenu
import GoogleSignIn


class HomeController: UIViewController {

    //Properties
    var delegate: HomeMenuControllerDelegate?
    var welcomeLabel: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.font = UIFont.systemFont(ofSize: 28)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.alpha = 0
        return label
    }()
    //Map Button
//    let gotoMap: UIButton = {
//        let button = UIButton(type: .system)
//        button.setImage(#imageLiteral(resourceName: "Image-1").withRenderingMode(.alwaysOriginal), for: .normal)
//        button.addTarget(self, action: #selector(handleShowMap), for: .touchUpInside)
//        return button
//    }()
    //Map Change View
//    @objc func handleShowMap() {
//        navigationController?.pushViewController(MapController(), animated: true)
//    }

    //Init

    override func viewDidLoad() {
        super.viewDidLoad()
        authenticateUserAndConfigureView()
        let newViewController = TabBarController()
        self.navigationController?.pushViewController(newViewController, animated: true)



    }



    //Selectors
    @objc func handleShowSideBar() {
        navigationController?.pushViewController(MenuController(), animated: true)
    }

    @objc func handleShowTabBar() {
        navigationController?.pushViewController(TabBarController(), animated: true)
    }



    @objc func handleSignOut() {
        let alertController = UIAlertController(title: nil, message: "Are you sure you want to sign out?", preferredStyle: .actionSheet)
        alertController.addAction(UIAlertAction(title: "Sign Out", style: .destructive, handler: { (_) in
            self.signOut()
        }))
        alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        present(alertController, animated: true, completion: nil)
    }

    @objc func handleMenuToggle() {
        delegate?.handleMenuToggle(forMenuOption: nil)
    }

    @objc func handleTabBar() {
        let handleTabBar = TabBarController()
        self.navigationController?.pushViewController(handleTabBar, animated: true)
        self.present(handleTabBar, animated: true, completion: nil)
    }

    //API

    func loadUserData() {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        Database.database().reference().child("users").child(uid).child("username").observeSingleEvent(of: .value) { (snapshot) in
            guard let username = snapshot.value as? String else { return }
            self.welcomeLabel.text = "Welcome, \(username)"

            UIView.animate(withDuration: 0.5, animations: {
                self.welcomeLabel.alpha = 1
            })
        }
    }

    func signOut() {
        do {
            try Auth.auth().signOut()
            let navController = UINavigationController(rootViewController: LoginController())
            navController.navigationBar.barStyle = .black
            self.present(navController, animated: true, completion: nil)
        } catch let error {
            print("Failed to sign out with error..", error)
        }
    }

    func authenticateUserAndConfigureView() {
        if Auth.auth().currentUser == nil {
            DispatchQueue.main.async {
                let navController = UINavigationController(rootViewController: LoginController())
                navController.navigationBar.barStyle = .black
                self.present(navController, animated: true, completion: nil)
            }
        } else {
            configureViewComponents()
            loadUserData()
        }
    }


    //Helper Functions

    func configureViewComponents() {
        view.backgroundColor = UIColor.mainPurple()

        navigationItem.title = "Artify"

        navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "baseline_clear_white_36pt_3x"), style: .plain, target: self, action: #selector(handleSignOut))
       navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_menu_white_3x"), style: .plain, target: self, action: #selector(handleShowSideBar))

        navigationItem.backBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "baseline_arrow_back_white_24dp"), style: .plain, target: self, action: #selector(handleShowTabBar))

        navigationItem.rightBarButtonItem?.tintColor = .white
        navigationController?.navigationBar.barTintColor = UIColor.mainPurple()

        navigationItem.leftBarButtonItem?.tintColor = .white
        navigationController?.navigationBar.barTintColor = UIColor.mainPurple()

        navigationItem.backBarButtonItem?.tintColor = .white
        navigationController?.navigationBar.barTintColor = UIColor.mainPurple()

        view.addSubview(welcomeLabel)
        welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        //Map Icon

//        view.addSubview(gotoMap)
//        gotoMap.anchor(top: view.topAnchor, left: nil, bottom: nil, right: nil, paddingTop: 210, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 100, height: 100)
//        gotoMap.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    }

}


这是标签栏控制器


import UIKit

class TabBarController:UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self

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

        // Create Tab one
        let Map = MapController()
        let MapItem = UITabBarItem(title: "Map", image: UIImage(named: "Image-7"), selectedImage: UIImage(named: "Image-7"))

        Map.tabBarItem = MapItem


        // Create Tab two
        let Trending = TrendingController()
        let TrendingBarItem2 = UITabBarItem(title: "Trending", image: UIImage(named: "profile_grey.png"), selectedImage: UIImage(named: "profile_grey.png"))

        Trending.tabBarItem = TrendingBarItem2

        let RecentlySold = RecentlySoldController()
        let RecentlySold3 = UITabBarItem(title: "Recently Sold", image: UIImage(named: "profile_grey.png"), selectedImage: UIImage(named: "profile_grey.png"))

        RecentlySold.tabBarItem = RecentlySold3


        self.viewControllers = [Trending, RecentlySold, Map]
    }


    // UITabBarControllerDelegate method
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
       // print("Selected \(viewController.title!)")
    }
}




我要完成的主要结果是,当我进入主页时,选项卡栏也会显示。

最佳答案

请记住,代码不会停止。你是说:

override func viewDidLoad() {
    super.viewDidLoad()
    authenticateUserAndConfigureView()
    let newViewController = TabBarController()
    self.navigationController?.pushViewController(newViewController, animated: true)
}


这并不意味着“做authenticateUserAndConfigureView并等待”。该代码不等待。它只是匆匆向前并推动了标签栏控制器。如果您希望用户看到您的HomeController,请删除最后两行。

但是,通常的做法是让选项卡栏成为家庭控制器。这样,这是用户看到的第一件事。然后,您可以在其前面显示身份验证视图。

或者,您可以选择(在applicationDidFinishLaunching中)使用户看到的第一件事是HomeController或选项卡栏控制器,这取决于是否进行了身份验证。

关于swift - 如何以编程方式将选项卡栏包含在主家用 Controller 中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55699772/

10-11 00:26