简体   繁体   English

如何使视图具有半透明边框并在下面显示视图

[英]How to make a view have a semi-transparent border and show the view below

Probably a bit of a newbie question, but .... I am writing an iPhone app which uses UITabBarController. 可能是一个新手问题,但是.....我正在编写一个使用UITabBarController的iPhone应用程序。 In Interface Builder I've setup the tab bar in MainWindow.xib. 在Interface Builder中,我已经在MainWindow.xib中设置了标签栏。 I have 4 tabs, and each one is set to load the xib for the appropriate UIViewController subclass. 我有4个标签,每个标签都设置为为适当的UIViewController子类加载xib。 I have created the views in the xib files for each UIViewController subclass in Interface Builder. 我已经在Interface Builder中为每个UIViewController子类的xib文件中创建了视图。 All is working well in that I can tap each tab and it shows the view for the correct UIViewController 一切运行良好,因为我可以点击每个选项卡,并且显示正确UIViewController的视图

But what I really want is for the view for one of the UIViewController subclasses to have a semi-transparent border of approx 30px on all 4 edges, so that it shows the edges of the view behind, kind of greyed out. 但是我真正想要的是让其中一个UIViewController子类的视图在所有4条边上都具有大约30px的半透明边框,这样它就可以将视图的边显示为灰色。 IE. IE浏览器。 the first tab is the main application, and that takes up the whole screen (minus the status and tab bar areas).Tab 2 is to save, and I want it to look like a small modal window over the top of the main app. 第一个选项卡是主应用程序,它占据了整个屏幕(减去状态和选项卡栏区域)。选项卡2用于保存,我希望它看起来像是主应用程序顶部的小模式窗口。 (If I were doing this as a html web app, the terminology and technology I'd be using would be a jQuery overlay) (如果我是作为html Web应用程序这样做的,那么我将使用的术语和技术将是jQuery叠加层)

Does this make sense? 这有意义吗?

I've tried playing with presentModalViewController but this makes it worse in that it takes up the entire screen including the status and tab bar areas. 我尝试过使用presentModalViewController进行播放,但这会使情况变得更糟,因为它占用了整个屏幕,包括状态和标签栏区域。

Any help or pointers very much appreciated Cheers 任何帮助或指点非常感谢

Nathan 弥敦道

Your UIViewController cannot be transparent to the view below it because the iphone may unload the view below it that is not currently being shown (to save memory). 您的UIViewController对其下面的视图不能透明,因为iphone可能会卸载当前未显示的下面的视图(以节省内存)。

The best solution I have used is to take a picture of the current view before you push your new view controller and then use that as the background image (fake the transparency). 我使用的最好的解决方案是在按下新的视图控制器之前为当前视图拍照,然后将其用作背景图像(假透明)。 Here's how I implemented this: 这是我的实现方式:

NewViewController *newView = [[NewViewController alloc] init];
shareVC.imageBackground = [Utilities getScreenshot:self.view];
[self presentModalViewController:newView animated:YES];
[newView release];  

then on your newViewController do this (on viewDidLoad, viewWillAppear, etc): 然后在newViewController上执行此操作(在viewDidLoad,viewWillAppear等上):

[imageView setImage:imageBackground];

and here's the screenshot function 这是屏幕截图功能

+(UIImage *)getScreenshot:(UIView *)_view {
    //take a screenshow of the parent view so we can add it as a background to the modal view
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(_view.window.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(_view.window.bounds.size);
    [_view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

You then need to setup your new view with a UIImageView as the background and pick the right Alpha value for that imageView to make it appear like it's transparent. 然后,您需要使用UIImageView作为背景来设置新视图,并为该imageView选择正确的Alpha值以使其看起来像透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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