如何在所有视图的顶部添加UIView?

我有这样的代码:

@interface Alert : UIView { } /* - (void) initWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString*)buttonTitle,... delegate:(id)delegate; */ @end @implementation Alert - (void) drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextClearRect(context, rect); // Очистим context CGContextSetRGBFillColor(context, 255, 0, 0, 1); CGContextFillRect(context, CGRectMake(20, 20, 100, 100)); } - (void) show { self.center = [[[UIApplication sharedApplication] keyWindow] center]; [[[UIApplication sharedApplication] keyWindow] addSubview:self]; [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:self]; } 

那么我就这样使用它:

 - (void)viewDidLoad { [super viewDidLoad]; Alert * alert = [[Alert alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; [alert show]; [alert release]; } 

但它不起作用(我想看到警报高于所有其他意见)。 你可以帮帮我吗?

正确处理设备方向的推荐解决scheme是

  • 荚AGWindowView它会自动处理任何旋转和framechanges,所以你不必担心。
  • 阅读并按照官方posthttps://developer.apple.com/library/content/qa/qa1688/_index.html
  • 将您的视图添加到第一个子视图,而不是将其添加到窗口

     UIWindow* window = [UIApplication sharedApplication].keyWindow; if (!window) window = [[UIApplication sharedApplication].windows objectAtIndex:0]; [[[window subviews] objectAtIndex:0] addSubview:myView]; 

您也可以将其添加到窗口。 不过,它不会处理设备方向。

 let window = UIApplication.sharedApplication().keyWindow! let view = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height)) window.addSubview(v); view.backgroundColor = UIColor.blackColor() 

还有另一种方法

在视图中添加以下方法会出现添加每次推/popup视图控制器

 -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[[UIApplication sharedApplication] keyWindow] addSubview:<yourViewObject>]; } 

为了在下一课中删除这个视图,添加以下几行代码

 -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [<yourViewObject> removeFromSuperview]; } 

@MaciekWrocławanswere swift版本

Swift2

  var appDelegate = (UIApplication.sharedApplication().delegate! as! AppDelegate) var backgrView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height)) backgrView.backgroundColor = UIColor.redColor() backgrView.alpha = 0.6 window?.addSubview(backgrView) 

swift3

  var appDelegate: AppDelegate? = (UIApplication.shared.delegate as? AppDelegate) var backgrView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(UIScreen.main.bounds.size.height))) backgrView.backgroundColor = UIColor.red backgrView.alpha = 0.6 window?.addSubview(backgrView) 

我正在做我的应用程序有关networking连接在AppDelegate中:

 func checkReachabilityUpdateUI() -> Int { let remoteHostStatus = self.reachability?.currentReachabilityStatus() if remoteHostStatus?.rawValue == 0 { let backgrView = UIView(frame: CGRectMake(0, 60, UIScreen.mainScreen().bounds.size.width, 44)) let statusMessage = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: 44)) statusMessage.text = "No internet Connection" statusMessage.textAlignment = .Center statusMessage.textColor = UIColor.whiteColor() backgrView.backgroundColor = UIColor.redColor() backgrView.addSubview(statusMessage) backgrView.tag = 100 window?.addSubview(backgrView) return (remoteHostStatus?.rawValue)! } else { if let viewWithTag = self.window!.viewWithTag(100) { print("Tag 100") viewWithTag.removeFromSuperview() } else { print("tag not found") } return (remoteHostStatus?.rawValue)! } } 

如果您知道顶部的子视图,请使用:

 UIView* topMostSubView = <your view's top most subview> [self.view insertSubview:alert aboveSubview:topMostSubView];