推送具有透明背景的ViewController

时间:2016-01-25 13:38:13

标签: ios objective-c iphone xcode uiviewcontroller

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

HostViewController(带背景图像和VisualEffectView)
- NavigationController(通过HostViewController模态呈现)
- ViewController(具有清晰的背景色)

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

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

先谢谢Fabian。


修改 为了澄清我的问题,我添加了这个gif:

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



我想要达到的目的:

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

1 个答案:

答案 0 :(得分:8)

我有这样的解决方案,但我不认为这是最好的解决方案,请记住这只是一种解决方法。

首先要做的是设置global背景,如:

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

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

    return YES;
}

blur.jpeg是一个清晰(不模糊)的图像我正在使用它(例如从谷歌获取)

enter image description here

接下来是将UIViewController子类化为全球使用,但由您决定如何全局实现它。

//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

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

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

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

@interface ViewController : BGBlurViewController

@end

和另一个:

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

@interface ViewController2 : BGBlurViewController

@end

注意: 我在故事板中将UIViewController(ViewController和ViewController2)的backgroundColor设置为Default / none,这只是一个解决方法......

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

enter image description here

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