簡體   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