简体   繁体   中英

How to add a fullscreen subview to the current UIWindow in objective c which will change according to the device orientation?

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.

try this

_autoPlayViewBackground.frame=self.view.frame;

into

_autoPlayViewBackground.view.frame = window.bounds; // or use window .frame
_autoPlayViewBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

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.

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

Follow this pattern:

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

[parentView addSubview:subView];

Hope this helps!

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM