首先添加一个UIView,甚至是导航栏

问题描述 投票:165回答:16

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

  • 全屏黑色背景与0.5 alpha看到下面的其他UIViewController
  • 中间有一个UIView窗口,里面有一些信息,(如果你想了解一切的日历)。

为此,我创建了一个包含两个UIViews(背景和窗口)的UIViewController,我正在尝试显示它。我尝试了一个简单的[mySuperVC addSubview:myPopUpVC.view],但我仍然有上面的导航栏。

我试图将它作为模态呈现,但下面的UIViewController消失了,我失去了透明效果。

任何想法,我相信这很简单......

谢谢!

ios uiviewcontroller uinavigationbar popupwindow
16个回答
183
投票

您可以通过将视图直接添加到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)

5
投票

@ Nam的答案非常有用,如果您只想显示自定义视图,但如果您的自定义视图需要用户交互,则需要禁用navigationBar的交互。

self.navigationController.navigationBar.layer.zPosition = -1
self.navigationController.navigationBar.isUserInteractionEnabled = false

就像Nam在答案中所说的那样,不要忘记扭转这些变化:

self.navigationController.navigationBar.layer.zPosition = 0
self.navigationController.navigationBar.isUserInteractionEnabled = true


You can do this in a better way with an extension:
extension UINavigationBar {
    func toggle() {
        if self.layer.zPosition == -1 {
            self.layer.zPosition = 0
            self.isUserInteractionEnabled = true
        } else {
            self.layer.zPosition = -1
            self.isUserInteractionEnabled = false
        }
    }
}

只需像这样使用它:

self.navigationController.navigationBar.toggle()

4
投票
[[UIApplication sharedApplication].windows.lastObject addSubview:myView];

2
投票

请注意,如果您想在全屏幕中添加视图,请仅使用以下代码

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

2
投票
UIApplication.shared.keyWindow?.insertSubview(yourView, at: 1)

此方法适用于xcode 9.4,iOS 11.4


1
投票

我将使用一个UIViewController子类,其中包含一个嵌入其他视图控制器的“容器视图”。然后,您就可以在Container视图中添加导航控制器(例如,使用嵌入关系segue)。

Implementing a Container View Controller


1
投票

在Swift 4.2和Xcode 10中

var spinnerView: UIView? //This is your view

spinnerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
//Based on your requirement change width and height like self.view.bounds.size.width
spinnerView?.backgroundColor = UIColor.black.withAlphaComponent(0.6)
//        self.view.addSubview(spinnerView)
let currentWindow: UIWindow? = UIApplication.shared.keyWindow
currentWindow?.addSubview(spinnerView!)

在目标C中

UIView * spinnerView; //这是你的观点

self.spinnerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];  
//Based on your requirement change width and height like self.view.bounds.size.width
self.spinnerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
// [self.view addSubview:self.spinnerView];
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:self.spinnerView];

这可以仅适用于纵向或横向模式。

一个更简单的代码是:

yourViewName.layer.zPosition = 1//Change you view name

0
投票
[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];

enter image description here


126
投票

[self.navigationController.view addSubview:overlayView];是你真正想要的


99
投票

这是一个适合我的简单优雅的解决方案。您可以将导航栏的z位置设置为视图下方:

self.navigationController.navigationBar.layer.zPosition = -1;

记得在完成后将其设置为0。


57
投票

检查响应的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)

22
投票

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

[self.navigationController.navigationBar addSubview: overlayView)]

您也可以在窗口上添加它:

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

希望这可以帮助.. :)


19
投票

Dalef在swift中的出色解决方案:

self.navigationController?.view.addSubview(view)

8
投票

Swift版@Nicolas Bonnet的回答:

    var popupWindow: UIWindow?

func showViewController(controller: UIViewController) {
    self.popupWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
    controller.view.frame = self.popupWindow!.bounds
    self.popupWindow!.rootViewController = controller
    self.popupWindow!.makeKeyAndVisible()
}

func viewControllerDidRemove() {
    self.popupWindow?.removeFromSuperview()
    self.popupWindow = nil
}

不要忘记窗口必须是强大的属性,因为原始答案会导致窗口立即释放


6
投票

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

希望有所帮助!


6
投票

有多种方法可以做到:

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

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

2-使用自定义转换,使你的UIViewController在后面显示0.5 alpha

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

© www.soinside.com 2019 - 2024. All rights reserved.