簡體   English   中英

如何在目標c中向當前UIWindow添加全屏子視圖,該子視圖將根據設備方向而變化?

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

我想在當前呈現的viewcontroller上添加一個覆蓋整個屏幕的臨時視圖,經過研究后發現我應該將該屏幕添加為當前UIWindow的子視圖,我使用了以下代碼,但是該視圖並未覆蓋整個屏幕屏幕。

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

這段代碼提供了一個通向一面的窗口。任何幫助都將是一個很大的幫助。

嘗試這個

_autoPlayViewBackground.frame=self.view.frame;

進入

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

這個怎么樣?

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

確保要顯示的內容將添加到myView UIView中。 這是非常重要的一步。

請嘗試給定的方法以全屏顯示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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM