Open UIViewController modally from tab bar with transparent background and transition style

193 views Asked by At

I've got a custom tab bar that I created using this link: https://github.com/jain-mohit/CustomTabBar. Now, I want to open a view modally after pressing a button. The opened view must be transparent, meaning I can see the view beneath it. The modal view also must a navigation bar so I can put a "Cancel" button to dismiss it. I've tried it like this:

DetailsViewController *detailViewController = [[DetailsViewController alloc] init];


UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:detailViewController];
nav.modalPresentationStyle = UIModalPresentationCurrentContext;

nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
detailViewController.view.backgroundColor = [UIColor greenColor];
detailViewController.view.alpha = 0.5f;

[self  presentViewController:nav animated:YES completion:nil];

This method quite works but when opening, the view doesn't transition from bottom to top. And I also got this error:

Presenting view controllers on detached view controllers is discouraged <UINavigationController: 0x8f52280>

So, I'm looking for another way to do this. Hopefully someone can help. This method should work for iOS6, 7 and 8. Also I'm not using storyboard to do this.

Thank You.

1

There are 1 answers

0
Nadzeya On

Try to use UIWindow, instead of presenting a new ViewController:

UIWindow *w = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
w.windowLevel = UIWindowLevelAlert;
w.autoresizesSubviews = NO;
w.clipsToBounds = YES;
w.hidden = NO;
[w addSubview:detailViewController];

In this case background of your new ViewController will be transparent, but you can't use UINavigationController here...