简体   繁体   English

无法将一个视图控制器显示在另一个视图控制器上

[英]Can't show one view controller on top of another

I am trying to show one view controller on top of another from a UITableView. 我试图显示一个视图控制器从UITableView的另一个。 When the user clicks one of the buttons, I would like a view controller to appear over the view controller where the user was when they pressed the button. 当用户单击按钮之一时,我希望视图控制器出现在用户按下按钮时所在的视图控制器上方。 So say the user is in the feed view, when they click the button, a view controller would slide over the feed view controller with a blur. 可以这样说,用户处于提要视图中,当他们单击按钮时,视图控制器将在提要视图控制器上滑动并带有模糊效果。 So basically the old view controller would act as a blurred background for the new one. 因此,基本上,旧的视图控制器将充当新视图控制器的模糊背景。 I am trying to achieve something like tumblr does. 我正在尝试实现类似tumblr的功能。

Here is my code in AppDelegate: 这是我在AppDelegate中的代码:

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if ([viewController.title isEqualToString:@"Create"]) {


        CreateOptionViewController *createOptionViewController = [[CreateOptionViewController alloc] init];
        [viewController addChildViewController: createOptionViewController];
        createOptionViewController.view.frame = viewController.view.bounds;
        [viewController.view addSubview: createOptionViewController.view];
        [viewController.view bringSubviewToFront:createOptionViewController.view];
        [createOptionViewController didMoveToParentViewController: viewController];

        return NO;

    }

    return YES;
}

Here is what I am trying to achieve: 这是我要实现的目标:

在此处输入图片说明

The viewController passed into this method is the view controller that ends up being selected. 传递给此方法的viewController是最终被选择的视图控制器。 If this returns "NO" then that view controller never becomes selected. 如果返回“ NO”,则该视图控制器将永远不会被选中。

For example, you have a tab bar controller with 2 tabs and "tab 1" is selected by default. 例如,您有一个带有2个标签的标签栏控制器,并且默认情况下选择了“标签1”。 If the user tapped on the "tab 2", the function below would be called and "viewController" would be the view controller that belongs to "tab 2." 如果用户点击“选项卡2”,则下面的函数将被调用,“ viewController”将是属于“选项卡2”的视图控制器。 If this function returns YES, then the view controller attached to "tab 2" is brought to the front. 如果此函数返回“是”,则将附加到“选项卡2”的视图控制器置于最前面。 If the function returns "NO" then nothing happens. 如果函数返回“ NO”,则什么也不会发生。 Meaning the view controller associated with "tab 2" is not displayed. 表示未显示与“选项卡2”关联的视图控制器。

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

Your code attempts to add a child view controller to the view controller that will not be displayed. 您的代码尝试将子视图控制器添加到将不会显示的视图控制器。 Since the method returns "NO" "viewController" isn't front and foremost. 由于该方法返回“ NO”,因此“ viewController”不是最重要的。

 [viewController addChildViewController: createOptionViewController];

You need to add "createOptionViewController" as a childViewController to a view controller that is currently being displayed. 您需要将“ createOptionViewController”作为childViewController添加到当前正在显示的视图控制器中。 In this case you'll probably want to add it as 在这种情况下,您可能希望将其添加为

tabBarController.selectedViewController

    CreateOptionViewController *createOptionViewController = [[CreateOptionViewController alloc] init];
    UIViewController *selectedVC = tabBarController.selectedViewController;

     [selectedVC addChildViewController: createOptionViewController];
     createOptionViewController.view.frame = viewController.view.bounds;
     [selectedVC.view addSubview: createOptionViewController.view];
     [selectedVC.view bringSubviewToFront:createOptionViewController.view];
     [createOptionViewController didMoveToParentViewController: viewController];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS无法从表视图控制器导航到另一个 - IOS can't navigate from a table view controller to another one 如果该View Controller不是立即被隔离到的ViewController,则无法使用prepareForSegue方法将数据传递给另一个View Controller - Can't pass data to another View Controller with the prepareForSegue method if that View Controller is not the one immediately being segued to 我们能否在导航栏上显示多个视图,而导航栏将不同的控制器作为另一视图的子视图? - Can we show more than one view with navigation bar having different controller as subview to another view? 在没有全屏的情况下在另一个视图控制器上显示视图控制器 - Present a view controller on top another one without full screen 我如何使用swift在不是当前的视图控制器中显示从数据库中检索到的数据,而在ios中显示另一个 - how can i show retrieved data from database in not a present view controller but another one in ios with using swift 无法从另一个视图控制器获取一个表视图数据 - Can't get one a table view data from another view controller 无法将视图控制器属性获取到另一个视图控制器 - can't get a view controller property to another view controller 在根视图控制器上看不到顶部导航栏 - Can't see Top Navigation Bar on Root View Controller iOS关闭了一个视图控制器,并在不显示基本视图控制器的情况下显示了另一个 - iOS dismiss a view controller and present another one without show the base view controller 在xcode中从一个视图控制器拖到另一个视图控制器时,“显示为弹出窗口”不会出现吗? - “show as popover” is not coming while dragging from one view controller to another view controller in xcode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM