简体   繁体   中英

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. 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.

Here is my code in 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. If this returns "NO" then that view controller never becomes selected.

For example, you have a tab bar controller with 2 tabs and "tab 1" is selected by default. 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." If this function returns YES, then the view controller attached to "tab 2" is brought to the front. If the function returns "NO" then nothing happens. Meaning the view controller associated with "tab 2" is not displayed.

-(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.

 [viewController addChildViewController: createOptionViewController];

You need to add "createOptionViewController" as a childViewController to a view controller that is currently being displayed. 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM