繁体   English   中英

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

[英]Add a view controller as a subview in another view controller

我已经阅读了此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)
    }

}

我有一个NavigationBartapBar 想要在container的视图内部显示viewController

在此处输入图片说明

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

在此处输入图片说明 在此处输入图片说明

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

我可以通过以下更改来解决您的问题:

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

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

在此处输入图片说明

在ViewController中添加以下代码:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        tabbar.invalidateIntrinsicContentSize()
    }

完成此操作后,将运行UITabBar

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM