在另一个视图控制器中添加视图控制器作为子视图

问题描述 投票:0回答:1

我已经读过这个LINK,但不适合我。我想在另一个viewController中显示viewController作为子视图。

这是我的代码 -

import UIKit
import CarbonKit

class ViewController: UIViewController, CarbonTabSwipeNavigationDelegate {

 @IBOutlet weak var containerView: UIView!

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let items = ["All",  "WOMEN",  "MEN",  "KIDS",  "HOME",  "CITY"]
        let carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: items, delegate: self)
        carbonTabSwipeNavigation.insert(intoRootViewController: self)
    }

    func carbonTabSwipeNavigation(_ carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAt index: UInt) -> UIViewController {

        //           let screen = self.storyboard?.instantiateViewController(withIdentifier: "demo") as! demo
        //           showSubViewContrller(subViewController: vc)
        //           return screen

        let storyBoard = getStoryBoardByIndentifier(identifier: "All")
        let vc = storyBoard.instantiateViewController(withIdentifier: "AllViewController") as! AllViewController
        showSubViewContrller(subViewController: vc)
        return vc
    }

    //Subview Controller
    func showSubViewContrller(subViewController:UIViewController) {
        self.addChildViewController(subViewController)
        subViewController.view.frame = containerView.frame
        self.containerView.addSubview(subViewController.view)
        subViewController.didMove(toParentViewController: self)
    }

    func getStoryBoardByIndentifier(identifier:String)->UIStoryboard {
        return  UIStoryboard.init(name: identifier, bundle: nil)
    }

}

我有一个NavigationBar和一个tapBar。想在viewController的视图中显示container

enter image description here

但是当视图加载它时,它会覆盖/隐藏tabBar。

enter image description here enter image description here

如何解决这个问题并在我指定的容器中显示viewController。项目链接 - GitHub

ios swift carbonkit
1个回答
1
投票

不知怎的,我可以通过以下更改解决您的问题:

carbonTabSwipeNavigation.insert(intoRootViewController: self)中的carbonTabSwipeNavigation.insert(intoRootViewController: self, andTargetView: containerView)替换此方法viewDidLoad

注意:将UITaBar底部约束赋予SuperView而不是SafeArea:

enter image description here

在ViewController中添加以下代码:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        tabbar.invalidateIntrinsicContentSize()
    }

在你执行此操作后,你将UITabBar

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.