簡體   English   中英

如何加載具有透明背景色的UIViewController?

[英]How to load UIViewController with transparent Background color?

如何加載具有透明背景色的UIViewController? 這樣就可以看到以前的視圖控制器。 我嘗試使用以下方法創建自定義視圖,但是它不起作用。

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    {
        CGColorRef colorRef=[UIColor clearColor].CGColor;
        CGContextSetFillColorWithColor(ctx, colorRef);
//        CGColorRelease(colorRef);
        CGContextFillRect(ctx, rect);
    }
    CGContextRestoreGState(ctx);
}

任何幫助將不勝感激!

您可以通過添加子視圖來實現。

//Get your screen size
CGRect screenBounds = [[UIScreen mainScreen] bounds];
//Create the transparent view of the same size
UIView* transparentView = [[UIView alloc] initWithFrame:screenBounds];
// change the background color to black and the opacity to 0.5 as this will make it look transparent
transparentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
// add this new view to your main view
[self.view addSubview:transparentView];

然后,可以在此視圖中添加所有UI元素。 這將使它看起來像是將透明玻璃放在舊視圖上。

完成后,您只需從超級視圖中刪除即可。

[transprentView removeFromSuperview];

編輯:

或者您也可以像這樣回答

希望有幫助,朱利安

使您的視圖控制器的視圖透明viewController.view.backgroundColor = [UIColor clearColor];

但是,在任何情況下都無法幫助您查看其背后的視圖控制器內容。 例如,如果您的控制器位於導航控制器中,則不會。 在這種情況下-在將新控制器推到頂部之前,請使用方法制作快照視圖,並將其用作頂部控制器所有內容下的一層。

喜歡

 UIView *backgroundView = [navVC.topViewController.view snapshotViewAfterScreenUpdates:NO];

 [nextVC.view insertSubview:backgroundView atIndex:0];
 [navVC psuhViewController:nextVC animated:YES];

暫無
暫無

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

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