[英]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
. 我有一个NavigationBar
和tapBar
。 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.