[英]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)
}
}
我有一個NavigationBar
和tapBar
。 想要在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.