【问题标题】:How to add and open a nib as child viewcontroller progrmatically from UITabBarController如何从 UITabBarController 以编程方式添加和打开笔尖作为子视图控制器
【发布时间】:2020-04-16 14:25:08
【问题描述】:

我有一个 UITabBarController 和四个与之关联的 UIViewController。我在 UITabBarController 子类中的导航栏位置创建了一个自定义视图,所以这个自定义视图对于所有 UIViewController 都是通用的。当点击其中一个按钮时,自定义视图中有两个按钮,我想将“FilesViewController.xib”作为子视图控制器添加并打开到当前活动的 UIViewController。

以下是我到目前为止尝试过的,它没有将 FilesViewController.xib 添加为子视图控制器。我做错了什么?

import UIKit

class RootTabBarController: UITabBarController {


    var topBarHeight:CGFloat = 87.0



    override func viewDidLoad() {
        super.viewDidLoad()
        let containerView = UIView()
        self.view.addSubview(containerView)
        containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: topBarHeight)
        containerView.backgroundColor = UIColor.init(red: 113.0/255.0, green: 193.0/255.0, blue: 34.0/255.0, alpha: 1.0)

        let filesButton = UIButton()
        containerView.addSubview(filesButton)
        filesButton.frame = CGRect(x: logoutButton.frame.origin.x-46, y: 40, width: 40, height: 40)
        filesButton.setImage(UIImage(named: "folder"), for: .normal)
        filesButton.addTarget(self, action: #selector(openFileViewController(_:)), for: .touchUpInside)

    }


    func configureFilesController() // Old Function
    {

        let filesController = FilesViewController()
        self.addChild(filesController)
        self.view.addSubview(filesController.view)
        filesController.didMove(toParent: selectedViewController)
        let height = view.frame.height
        let width = view.frame.width
        filesController.view.frame = CGRect(x: 0, y: self.view.frame.maxY, width: width, height: height) 

    }

func configureFilesController() // New One
    {
      filesController = FilesViewController.init(nibName: "FilesViewController", bundle: nil)
            self.addChild(filesController)
            self.view.addSubview(filesController.view)
            let height = view.bounds.height
            let width = view.bounds.width
            filesController.view.frame = CGRect(x: 0, y: height/2, width: width, height: height/2)
            filesController.view.layer.cornerRadius = 5.0
            filesController.didMove(toParent: self)

        }


    @objc func openFileViewController(_ sender: UIButton) {
       print("Tapped")
        configureFilesController()
    }

}

import UIKit

class FilesViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
    }

    override func viewWillAppear(_ animated: Bool) {
        prepareBackGroundView()
    }



    override func viewDidAppear(_ animated: Bool) { 
            super.viewDidAppear(animated)
// Adding the below line worked like what I was expecting
            self.view.frame = CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: self.view.frame.height) 
            UIView.animate(withDuration: 0.3) { [weak self] in
                let frame = self?.view.frame
                let yComponent = UIScreen.main.bounds.height - 200
                self?.view.frame = CGRect(x: 0, y: yComponent, width: frame!.width, height: frame!.height)
            }

        }

    func prepareBackGroundView(){
        let blurEffect = UIBlurEffect.init(style: .dark)
        let visualEffect = UIVisualEffectView.init(effect:blurEffect)
        let blurView = UIVisualEffectView.init(effect: blurEffect)

        blurView.contentView.addSubview(visualEffect)
        visualEffect.frame = UIScreen.main.bounds
        blurView.frame = UIScreen.main.bounds

        view.insertSubview(blurView, at: 0)
    }


}

【问题讨论】:

    标签: ios swift uiviewcontroller uitabbarcontroller


    【解决方案1】:

    您可以在stackoverflow answer 找到解决方案。通过使用这个答案,我创建了示例程序。在这里,我添加了两个选项卡,一个是在故事板中创建的子控制器,另一个是在 xib 中创建的。

    我的RootTabBarController 看起来像

    import UIKit
    
    class RootTabBarController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let child1 = storyBoard.instantiateViewController(identifier: "Child1ViewController")
            let child2 = Child2ViewCOntroller.init(nibName: "Child2ViewCOntroller", bundle: nil)
    
            let tabItem1:UITabBarItem = UITabBarItem(tabBarSystemItem: .contacts, tag: 0) // customise TabBar with images and title
            let tabItem2:UITabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
            child1.tabBarItem = tabItem1
            child2.tabBarItem = tabItem2
    
            self.setViewControllers([child1,child2], animated: true)
    
        }
    }
    

    【讨论】:

    • FilesViewController 的视图没有显示出来,但它进入了 viewDidLoad
    • 找到我的更新解决方案,如果您遇到任何问题,请告诉我我分享 bitbucket url
    • 感谢您抽出时间帮助我。这是我这边的一个小错误,导致 FilesViewController 没有出现。我在 RootTabBarController 中更新了正确的函数,并在 FilesViewController 中添加了一行代码。您对 .init 的初始提示对我有所帮助,但是,上述解决方案以编程方式将视图控制器添加到数组中,并没有解决我的问题。如前所述,在添加了几行之后,我能够自己解决这个问题。再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2016-08-19
    • 1970-01-01
    相关资源
    最近更新 更多