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

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

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

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

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

谢谢!


当前回答

我建议你创建一个新的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;

希望对大家有所帮助!

其他回答

[[UIApplication sharedApplication].windows.lastObject addSubview:myView];

我建议你创建一个新的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];

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

添加这些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()
}

Swift版本的检查响应:

Swift 4:

let view = UIView()
view.frame = UIApplication.shared.keyWindow!.frame
UIApplication.shared.keyWindow!.addSubview(view)

Swift 3.1:

let view = UIView()
view.frame = UIApplication.sharedApplication().keyWindow!.frame 
UIApplication.sharedApplication().keyWindow!.addSubview(view)