简体   繁体   English

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

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

I have already read this LINK , but not working for me. 我已经阅读了此LINK ,但不适用于我。 I want to show a viewController as a subview in another viewController . 我想显示一个viewController作为另一个viewController的子视图。

Here is my code - 这是我的代码-

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

}

I have a NavigationBar and a tapBar . 我有一个NavigationBartapBar Would like to show the viewController inside the view in a container . 想要在container的视图内部显示viewController

在此处输入图片说明

But when the view loads it's coverUp/hide the tabBar. 但是,当视图加载时,它会掩盖/隐藏tabBar。

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

How to solve this and show the viewController in my specified container. 如何解决这个问题并在我指定的容器中显示viewController Project Link - GitHub 项目链接-GitHub

Somehow i am able to fix your issue with below changes: 我可以通过以下更改来解决您的问题:

Replace this method carbonTabSwipeNavigation.insert(intoRootViewController: self) with carbonTabSwipeNavigation.insert(intoRootViewController: self, andTargetView: containerView) in viewDidLoad viewDidLoad carbonTabSwipeNavigation.insert(intoRootViewController: self, andTargetView: containerView)carbonTabSwipeNavigation.insert(intoRootViewController: self, andTargetView: containerView)替换此方法carbonTabSwipeNavigation.insert(intoRootViewController: self)

Note : Give UITaBar bottom constraint to SuperView not SafeArea: 注意:将UITaBar底部约束赋予SuperView而不是SafeArea:

在此处输入图片说明

Add below code in ViewController: 在ViewController中添加以下代码:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        tabbar.invalidateIntrinsicContentSize()
    }

After doing this when you run you will UITabBar : 完成此操作后,将运行UITabBar

在此处输入图片说明

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

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