How to add UIView on the top of all views?

The recommended solutions which handle device orientation properly are

  • pod AGWindowView It will automatically deal with any rotation and framechanges so you won't have to worry about that.

  • read and follow the official post https://developer.apple.com/library/content/qa/qa1688/_index.html

  • Add your view to the first subview, instead of adding it to the window

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

You can also add it to the window. It will not handle device orientation, though.

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(view);
view.backgroundColor = UIColor.blackColor()

There is also one other method

Add the following method in the view will appear to add every time push/pop view controller

-(void)viewWillAppear:(BOOL)animated
{
     [super viewWillAppear:animated];

     [[[UIApplication sharedApplication] keyWindow] addSubview:<yourViewObject>];
}

And for remove this view in next class add the following lines of code

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [<yourViewObject> removeFromSuperview];
 }