IOS:如何在呈现的模态视图控制器之上放置一些视图?

时间:2012-03-25 10:23:13

标签: iphone objective-c xcode cocoa uiview

我有一个活动视图,我已经在AppDelegate类中添加了点击栏:

[self.mainTabBar.view addSubview: spinner];

当存在连接问题时,它在每个视图控制器中都可见并且正在旋转。 某些视图控制器上有一些按钮,使得呈现一些模态视图控制器。 该模态视图控制器与微调器重叠。如何使该微调器始终位于所有视图之上或至少位于该模态视图控制器之上? 我试图在视图控制器中做出这样的事情,它呈现模态视图控制器:

[self presentModalViewController:selectionViewController animated:YES];
[self.view bringSubviewToFront:[self.tabBarController.view viewWithTag:15]];

不起作用。

4 个答案:

答案 0 :(得分:61)

将视图添加到主窗口。

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview: spinner];

答案 1 :(得分:12)

虽然phix23的答案是正确的,但这是一个更完整的例子:

//The view you want to present
UIViewController *viewControllerYouWantToPresentOnTop = [[UIViewController alloc] initWithNibName:nil bundle:nil];

//Create transparent host view for presenting the above view
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *viewControllerForPresentation = [[UIViewController alloc] init];
[[viewControllerForPresentation view] setBackgroundColor:[UIColor clearColor]];
[[viewControllerForPresentation view] setOpaque:FALSE];
[mainWindow addSubview:[viewControllerForPresentation view]];

//Make your transparent view controller present your actual view controller
[viewControllerForPresentation presentViewController:viewControllerYouWantToPresentOnTop animated:TRUE];

当你不再需要这些时,请记得自己清理。

此代码可以在您的应用中的任何位置使用,甚至可以在库中使用:)

答案 2 :(得分:4)

应用程序通常在其整个生命周期内在单个窗口中显示其内容。 但是在某些情况下,可能会使用额外的窗口在其他所有内容之上添加内容。 Apple通过在单独的窗口中添加它来确保UIAlertView始终保持最佳状态。

UIView *contentView = [[UIView alloc] initWithFrame:contentFrame];
contentView.backgroundColor = [UIColor greenColor];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(x,y,contentFrame.size.width, contentFrame.size.height)];
window.windowLevel = UIWindowLevelAlert;
[window addSubview:contentView];
[window makeKeyAndVisible];

根据需要设置window.hidden = Yes or No来显示和隐藏您的窗口。 这将始终显示您的contentView在应用中的所有其他内容之上。

答案 3 :(得分:0)

模态控制器位于完全不同的层中,您不能使呈现控制器的任何子视图与其重叠。

使用带有微调器的UIAlertView。警报显示在一个甚至模态控制器重叠的层中。