我想在任何其他视图,甚至是导航栏上,显示一种“弹出”视图,看起来像这样:

全屏黑色背景,0.5 alpha,可以看到下面的另一个UIViewController。 中间有一个带有一些信息的UIView窗口,(如果你想知道一切,一个日历)。

为了做到这一点,我已经创建了一个UIViewController,它包含了两个uiview (background和window),我试图显示它。我尝试了一个简单的[mySuperVC addSubview:myPopUpVC。视图],但我仍然有上面的导航栏。

我试着把它表示为一个模态,但是下面的UIViewController消失了,我失去了透明效果。

有什么办法可以做到这一点吗,我相信这很简单……

谢谢!


当前回答

做这件事的方法不止一种:

1-在UIWindow上添加你的UIView而不是在UIViewController上添加。这样的话,它就会在一切之上。

   [[(AppDelegate *)[UIApplication sharedApplication].delegate window] addSubview:view];

2-使用自定义过渡,将保持你的UIViewController显示在后面的0.5 alpha

为此,我建议你看看这个:https://github.com/Citrrus/BlurryModalSegue

其他回答

你可以通过将你的视图直接添加到keyWindow来实现:

UIView *myView = /* <- Your custom view */;
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:myView];

更新—适用于Swift 4.1及以上

let currentWindow: UIWindow? = UIApplication.shared.keyWindow
currentWindow?.addSubview(myView)

iOS13及以上更新

已弃用keyWindow。你应该使用以下方法:

UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.addSubview(myView)

注意,如果你想在全屏中添加视图,那么只能使用下面的代码

添加这些UIViewController的扩展

public extension UIViewController {
   internal func makeViewAsFullScreen() {
      var viewFrame:CGRect = self.view.frame
      if viewFrame.origin.y > 0 || viewFrame.origin.x > 0 {
        self.view.frame = UIScreen.main.bounds
      }
   }
}

继续正常的子视图添加过程

现在用于添加UIViewController的viewDidAppear

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

     self.makeViewAsFullScreen()
}

我建议你创建一个新的UIWindow:

UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = viewController;
window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
window.opaque = NO;
window.windowLevel = UIWindowLevelCFShareCircle;
window.backgroundColor = [UIColor clearColor];

[window makeKeyAndVisible];

然后你可以在另一个UIViewController中管理你的视图。 拆除窗户:

[window removeFromSuperview];
window = nil;

希望对大家有所帮助!

[self.navigationController.navigationBar.layer setZPosition:-0.1];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 20, 35, 35)];
[view setBackgroundColor:[UIColor redColor]];
[self.navigationController.view addSubview:view];
[self.navigationController.view bringSubviewToFront:view];
self.navigationController.view.clipsToBounds = NO;
[self.navigationController.navigationBar.layer setZPosition:0.0];

将您的视图添加为NavigationController的子视图。

[self.navigationController.navigationBar addSubview: overlayView)]

你也可以把它加在窗户上:

UIView *view = /* Your custom view */;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:view];

希望这能有所帮助。:)