[英]How do I add a UIView to a UIViewController from the menu tab bar controller with swift?
So, I have an app which has as its root view controller a menu bar controller class which I named MenuTabBarController
, and which holds a number of UIViewControllers
. 所以,我有一个应用程序,它的根视图控制器有一个菜单栏控制器类,我命名为
MenuTabBarController
,它包含许多UIViewControllers
。 One of those view controllers is the homepage which I called HomeViewController
and which is displayed on app load since it occupies index 0 position. 其中一个视图控制器是我称之为
HomeViewController
的主页,它在app load上显示,因为它占据了索引0的位置。 What I'm trying to do is create a subview in the viewDidLoad
method of MenuTabBarController
, but place this subview within HomeViewController
. 我想要做的是在
MenuTabBarController
的viewDidLoad
方法中创建一个子视图,但将此子视图放在HomeViewController
。 Here's what I came up with 这就是我想出来的
let homeViewController = HomeViewController()//An instance of HomeViewController created at global level
class MenuTabBarController: UITabBarController,UITabBarControllerDelegate,UIPopoverPresentationControllerDelegate{
var mainBox: UIView!//This is the sub view reference declared as an optional
override func viewDidLoad()
super.viewDidLoad()
self.delegate = self
mainBox = UIView(frame: CGRectMake(0,0,200,200))//Initialize mainBox
homeViewController.view.addSubview(mainBox)//Attempt to add mainBox to homeViewController
}//End viewDidLoad
}//End class definition
Well the mainBox doesn't get added because when I attempt to run the app, a blank page stares at me. 那么mainBox没有被添加,因为当我尝试运行应用程序时,一个空白页面盯着我看。 If I add the mainBox to the menu bar's view like so
如果我将mainBox添加到菜单栏的视图中,就像这样
self.view.addSubview(mainBox)
it gets added. 它被添加。 How can I add it to the homeView though?
我怎么能把它添加到homeView呢?
You can also do it by accessing index of tabs by getting all controllers of tab bar controller. 您还可以通过获取选项卡栏控制器的所有控制器来访问选项卡索引来执行此操作。
(self.viewControllers[0] as! HomeViewController).view.addSubview(mainBox)
Hope it will work for you!!! 希望它对你有用!!!
You can't add subview to tabbar controller because it's manage all tabs, it's not visible as VC to users. 您无法将子视图添加到tabbar控制器,因为它管理所有选项卡,它不会作为VC显示给用户。 So if you want to ass view in any tac(VC) then you can add it in it's
viewdidload
method or If you want to add view from tabbarcntoller
class then you can get all view controllers in array by calling viewControllers
by self
. 因此,如果你想在任何tac(VC)中查看,那么你可以在它的
viewdidload
方法中添加它,或者如果你想从tabbarcntoller
类添加视图,那么你可以通过self
调用viewControllers
来获取数组中的所有视图控制器。 self.viewControllers[0]
is you first VC (Home in your case) where self.viewControllers[1]
should be second. self.viewControllers[0]
是你的第一个VC(在你的情况下为Home),其中self.viewControllers[1]
应该是第二个。
Hope this will help :) 希望这会有所帮助:)
It seems that you are creating an extra instance of the home view controller (that is never made visible btw.) and add the sub view into it. 您似乎正在创建一个额外的主视图控制器实例(从未使其成为可见的btw。)并将子视图添加到其中。
I think what you really want to do is to create and add the sub view in viewDidLoad of your HomeViewController
class. 我想你真正想做的是在
HomeViewController
类的viewDidLoad中创建和添加子视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.