ios - 如何从 UITabBarController 以编程方式添加和打开 Nib 作为 subview Controller

标签 ios swift uiviewcontroller uitabbarcontroller

我有一个 UITabBarController 和四个与之关联的 UIViewController。我在 UITabBarController 子类中的导航栏位置创建了一个自定义 View ,因此该自定义 View 对于所有 UIViewController 都是通用的。自定义 View 中有两个按钮,当点击其中一个按钮时,我想添加并打开“FilesViewController.xib”作为当前事件 UIViewController 的 subview Controller 。

下面是我到目前为止所尝试的,它没有将 FilesViewController.xib 添加为 subview Controller 。我做错了什么?

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)
    }


}

最佳答案

您可以在这里找到解决方案 stackoverflow answer 。通过使用这个答案,我创建了示例程序。在这里,我添加了两个选项卡,一个是在 Storyboard中创建的子 Controller ,另一个是在 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)

    }
}

关于ios - 如何从 UITabBarController 以编程方式添加和打开 Nib 作为 subview Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59486804/

相关文章:

ios - swift 未转义的控制字符

ios - 如何只为一个类启用底部导航栏

arrays - 使自定义类型符合仅具有下标的 RandomAccessCollection

arrays - 如何快速将数组转换为字符串?

ios - 下载图像和视网膜显示

ios - 如何比较我当前位置的纬度和日志(以 3 英里为半径)? Xcode

objective-c - 如何使用 Scrollview(或最佳实践)垂直和水平地进行 Card Deck 样式页面转换

swift - 如何在滚动 UITableView 期间制作动画

ios - iOS 8 中 self.view.subviews 为空

iphone - 在 UIView 中加载不同的 View Controller (就像 Xcode 的 iFrame)