[英]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.