简体   繁体   English

如何使用swift从菜单标签栏控制器向UIViewController添加UIView?

[英]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 . 我想要做的是在MenuTabBarControllerviewDidLoad方法中创建一个子视图,但将此子视图放在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.

相关问题 Swift-如何将Tab Bar和导航栏添加到单个视图控制器? - Swift-How do I add Tab Bar AND Navigation Bar to a single view controller? 如何在swift中设置标签栏控制器的默认选项卡 - How do I set the default tab for tab bar controller in swift 如何将UIView控制器添加到UIView - how do i add a UIView controller to a UIView 我应该使用UIViewController还是UIView? 制作自定义标签栏/导航控制器 - Should I use UIViewController or UIView? Making a custom tab bar/nav controller 在UITabBarController中,如何找出我的标签栏中的哪个UIViewController呈现了Swift 2.x中的当前UIViewController。 - In a UITabBarController, how do I find out which UIViewController in my Tab Bar presented the current UIViewController in Swift 2.x 如何以编程方式在我的标签栏控制器下添加UIView? - How can i add a UIView under my tab bar controller programatically? 如何在Swift中将UIViewController添加到UIScrollView - How Do I Add UIViewController to UIScrollView in Swift 如何在标签栏控制器中从一个视图控制器切换到另一个视图控制器并保留标签栏? - How within a tab bar controller do I segue from one view controller to another and retain the tab bar? 如何在 Swift 中向 Tab Bar Controller 添加表视图控制器 - How to add a table view Controller to Tab Bar Controller in Swift 如何从UIView访问UIViewController的按钮? - How do I access a UIViewController's button from a UIView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM