1

I want to add a temporary view over the current presented viewcontroller which covers the entire screen.After some research I found that I should add this screen as a subview of current UIWindow.I used the following code.But the view is not covering the entire screen.

`UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
//mainWindow.frame=self.view.frame;
_autoPlayViewBackground.frame=self.view.frame;
_autoPlayViewBackground.hidden=NO;
[mainWindow addSubview:_autoPlayViewBackground];` 

This code is giving a window towards a side.Any help would be a great help.

abhimuralidharan
  • 5,752
  • 5
  • 46
  • 70
  • Possible duplicate of [Orientation in a UIView added to a UIWindow](http://stackoverflow.com/questions/2508630/orientation-in-a-uiview-added-to-a-uiwindow) – hfossli Jan 28 '16 at 08:44

5 Answers5

2

How about this?

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

Make sure whatever you want to show will be added in UIView which is myView. This is very important step.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
2

try this

_autoPlayViewBackground.frame=self.view.frame;

into

_autoPlayViewBackground.view.frame = window.bounds; // or use window .frame
_autoPlayViewBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

Please try given method to show AutoPlayBgView in full screen

- (void) showAutoPlayBgViewInFullScreen
{
    UIWindow* window = [[UIApplication sharedApplication] keyWindow];

    // set frame of auto play bg view
    _autoPlayViewBackground.frame = window.frame;

    // add auto play bg view as subview in window
    [window addSubview: _autoPlayViewBackground];
}

Hope this code will help you.. Thanks

Neha Gupta
  • 157
  • 4
1

Follow this pattern:

CGRect rect = [[UIScreen mainScreen] bounds];
subView.frame = rect;

[parentView addSubview:subView];

Hope this helps!

Dharmesh Siddhpura
  • 1,610
  • 12
  • 22
  • `-[UIScreen mainScreen]` has been deprecated on iOS 16.0. Also if you are considering about supporting Split View or Stage Manager, avoid using `-[UIScreen mainScreen]`. – Jinwoo Kim Sep 30 '22 at 15:49
1

To add a fullscreen subview to the current UIWindow you can do like this.

    UIView *viewBg=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    [viewBg setBackgroundColor:[UIColor colorWithRed:1/255.f green:1/255.f blue:1/255.f alpha:0.4]];
    [self.view.window addSubview:viewBg];
Piyush
  • 1,534
  • 12
  • 32