如何将全屏子视图添加到 objective c 中的当前 UIWindow,它会根据设备方向而变化?

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

我想在当前呈现的 viewcontroller 上添加一个临时视图,它涵盖整个 screen.After 一些研究我发现我应该将此屏幕添加为当前 UIWindow.I 的子视图使用了以下 code.But 视图未覆盖整个屏幕。

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

此代码提供 window 对 side.Any 的帮助将是一个很大的帮助。

这个怎么样?

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

确保您要显示的任何内容都将添加到 UIView 中,即 myView这是非常重要的一步。

试试这个

_autoPlayViewBackground.frame=self.view.frame;

进入

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

请尝试给定的方法以全屏显示 AutoPlayBgView

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

希望此代码对您有所帮助..谢谢

遵循此模式:

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

[parentView addSubview:subView];

希望对您有所帮助!

要向当前 UIWindow 添加全屏子视图,您可以这样做。

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