推ViewController与透明背景 [英] Push ViewController with transparent background

查看:181
本文介绍了推ViewController与透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的应用中将模糊图像作为固定背景。我的ViewController结构是这样的:



HostViewController(带背景图像和VisualEffectView)

- NavigationController(通过HostViewController模态呈现)

- ViewController(具有清晰的背景颜色)



当我想从NavigationController推送到另一个ViewController(它也具有清晰的背景颜色)时,新的ViewController重叠了第一个ViewController,它通过新的ViewController可见。这看起来很奇怪和丑陋。我的问题是如何在没有ViewControllers相互叠加的情况下实现具有固定背景的推送动画?



您可以在应用程序TuneShell中看到此效果的示例可以在App Store找到。



提前致谢Fabian。









编辑:
为了澄清我的问题,我添加了这个gif:





正如你在gif中看到的那样,当我推送到Playlist-ViewController时,rootViewController在动画时通过新的Playlist-ViewController可见。我想解决这个问题。








我想要达到的目的:





接下来是继承 UIViewController 供全球使用,但由您决定如何全局实施。

  // BGBlurViewController.h 
//
@interface BGBlurViewController:UIViewController

@end

// BGBlurViewController.m
//
@implementation BGBlurViewController

- (void)viewDidLoad {
[super viewDidLoad];
//加载视图后进行任何其他设置。

UIToolbar * background = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
[background setBarStyle:UIBarStyleBlackTranslucent];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
}

@end

我正在使用 UIToolbar 作为 UIViewController 的背景并实现模糊效果。






这是如何使用子类 UIViewController (BGBlurViewController)

  // RootViewController 
//
#importBGBlurViewController.h

@interface ViewController:BGBlurViewController

@end

和另一个:

  //查看rootViewController旁边的
//
#importBGBlurViewController.h

@interface ViewController2:BGBlurViewController

@end

注意:
我正在设置<$ c的backgroundColor $ c> UIViewController s(ViewController和ViewController2)到故事板中的默认 / none,这只是一个解决方法......






以下是运行时的示例输出:





希望这会对你有所帮助..干杯..


I want to have a blurred image as a fixed background in my app. My ViewController structure is like this:

HostViewController (with background image and VisualEffectView)
– NavigationController (modally presented over the HostViewController)
–– ViewController (with clear background color)

When I want to push to another ViewController (which has also clear background color) from the NavigationController, the new ViewController overlaps the first ViewController which is visible through the new ViewController. That looks quite strange and ugly. My question is how I could achieve a push animation with a fixed background without the ViewControllers overlaying each other?

You can see a sample of this effect in the app "TuneShell" which can be found in the App Store.

Thanks in advance, Fabian.



EDIT: To clarify my problem I added this gif:

As you can see in the gif, when I push to the Playlist-ViewController, the rootViewController is visible through the new Playlist-ViewController while it's animating. I want to fix that.



WHAT I WANT TO ACHIEVE:

http://fabian-thies.tk/demo.gif

解决方案

I have a solution for this way back, but I don't think this is the best one and keep in mind that this is just a workaround.

First thing to do is set your global background like:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ..
    self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blur.jpeg"]];

    return YES;
}

blur.jpeg is a clear(not blurred) image I'm using this for example (got it from google)

Next is subclassing UIViewController for global use, but its up to you how to implement it globally.

//BGBlurViewController.h
//
@interface BGBlurViewController : UIViewController

@end

//BGBlurViewController.m
//
@implementation BGBlurViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIToolbar *background = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [background setBarStyle:UIBarStyleBlackTranslucent];
    [self.view addSubview:background];
    [self.view sendSubviewToBack:background];
}

@end

I'm using UIToolbar as a background of UIViewController and achieve blur effect.


This is how to use the subclassed UIViewController(BGBlurViewController)

//RootViewController
//
#import "BGBlurViewController.h"

@interface ViewController : BGBlurViewController

@end

and another:

//View next to rootViewController
//
#import "BGBlurViewController.h"

@interface ViewController2 : BGBlurViewController

@end

NOTE: I'm setting the backgroundColor of the UIViewControllers (ViewController and ViewController2) to Default/none in storyboard, again this is just a workaround...


Here is the sample output in runtime:

Hope this would help you.. Cheers..

这篇关于推ViewController与透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆