How to push view controller with transparency mode? - ios

I am trying to push a view controller with transparency mode.
This is my code:
SearchViewController * searchViewController= [self.storyboard instantiateViewControllerWithIdentifier:#"SearchViewController"];
searchViewController.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7f];
[self.navigationController pushViewController:searchViewController animated:YES];
Something i did mistake find out and help me to solve this problem.

In iOS the default color of background is black.
So you navigation controller has a black background.
If you push a controller, you current controller will be replaced by your new one. If the new one have a transparent color you will see the view of your navigation.
If I understand you want your first controller to be still visible ?
If yes, then you should use :
SearchViewController * searchViewController= [self.storyboard instantiateViewControllerWithIdentifier:#"SearchViewController"];
searchViewController.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7f];
[self presentViewController:searchViewController animated:YES completion:nil];
You can also use a custom segue to have the cor

You have to set modalPresentationStyle with overFullScreen value
let viewController = SearchViewController(nibName: "SearchViewController", bundle: nil)
viewController.modalPresentationStyle = .overFullScreen
present(viewController, animated: false, completion: nil)

Related

Transparent vc over TabBarVC in iOS > 8

There are many q/a's on this topic, many of them referring to older versions of iOS. The best answer I could find on the subject was this one.
It almost works for me, but when presenting this over a UITabBarViewController subclass, it only partially works: I get a nice, semi-transparent view during the presentation animation, but once the presentation animation completes, the presented VC becomes opaque again.
Coded in Objective-C, but I'm happy to read Swift answers...
- (void)showBusyWithCompletion:(void (^)(BOOL))completion {
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.5];
vc.view.opaque = NO;
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:vc animated:YES completion:^{
[self performSelector:#selector(hideBusy:) withObject:vc afterDelay:4];
}];
}
- (void)hideBusy:(UIViewController *)vc {
[self dismissViewControllerAnimated:vc completion:nil];
}
Again, the presenting VC is a UITabBarVC subclass, with nothing done to it except some code to record which tabs were visited. The presented vc is a vanilla view controller. Its view appears transparent red as it slides over, then turns opaque red (darker, like it's being blended with black) once the transition is complete.
How can I keep the background transparent after it appears?
It turns out that the answer I refer to above is correct, and I transcribed the setting improperly. To recap: this doesn't work (and the other answer didn't claim it did):
vc.modalPresentationStyle = UIModalPresentationCurrentContext;
...but either of these do:
vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
or as #TylerTheCompiler points out:
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;

Presenting a modal ViewController which has semi transparent background

I'm presenting a SecondViewController modally on FirstViewController and the SecondViewController has a semi transparent background (White color with 70 percent opacity).
The problem I'm facing is when I present SecondViewController, the view of FirstViewController remains visible until the SecondViewController has finished presenting.
This makes the UI look laggy. The behaviour I'm expecting is as soon as the SecondViewController is presented, the view for FirstViewController should be invisible, or gradually faded out before the view of SecondViewController appears.
Any help will be greatly appreciated!
The code I use for presenting is :
SecondViewController *cntrlr = (SecondViewController *)[[UIStoryboard activationStoryboard] instantiateViewControllerWithIdentifier:#“UserVC”];
[cntrlr setModalPresentationStyle:UIModalPresentationPopover];
[self presentViewController:cntrlr animated:YES completion:nil];
After iOS 3.2 there is a method to do this without any “tricks” – see the documentation for the modalPresentationStyle property. You have a rootViewController which will present the viewController. So here's the code to success:
viewController.view.backgroundColor = [UIColor clearColor];
rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[rootViewController presentModalViewController:viewController animated:YES];
With this method the viewController's background will be transparent and the underlying rootViewController will be visible.
SecondViewController *cntrlr = (SecondViewController *)[[UIStoryboard activationStoryboard] instantiateViewControllerWithIdentifier:#“UserVC”];
[cntrlr setModalPresentationStyle:UIModalPresentationPopover];
self.view.alpha = 0.0f;
[self.navigationController.navigationBar setHidden:YES];
[self presentViewController:cntrlr animated:YES completion:nil];
// Need to set FirstViewController alpha 1.0f after dismisViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.view.alpha = 1.0f;
[self.navigationController.navigationBar setHidden:NO];
}

Add semi-transparant background viewcontroller with search bar throughout app

need help in adding search bar ViewController which whose SearchBar will be in NaviagationBar with Back button (navigation search i achieved - self.navigationItem.titleView = searchBarView) throughout app, but until it has search i want to show previous ViewController in background with semi-transparent black color just like i achieved in Android :
i can add semi-transparent ViewController to current ViewController :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
vc.view.backgroundColor = [UIColor clearColor];
[vc setTransitioningDelegate:transitionController];
vc.modalPresentationStyle= UIModalPresentationCurrentContext;
[self presentViewController:vc animated:YES completion:nil];
but what it is doing that it opens SecondViewController in transparent without NavigationBar, the FirstViewController has NavigationBar
And when the SecondViewController is opened it should have SearchBar in NavigationBar and it shouldn't be Transparent as i achieved in Android.
There will be n number of ViewController which will add this same controller as Overlay Controller with NavigationBar and back button.
Please Help.
I found some solutions which involve taking a snapshot of the view and adding them to your navBar controller.
From Apple:
https://developer.apple.com/library/ios/qa/qa1817/_index.html
Other interesting option:
iOS iPhone is it possible to clone UIView and have it draw itself to two UIViews?
In you code snippet you just create new SecondViewController and present it like modal. It appears without navigation bar because you create it without Navigation Controller.
If you want to keep SecondViewController in the same navigation stack as previous ViewController with navigation bar and default Back button you should call:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
[self presentViewController:vc animated:YES completion:nil];
[self.navigationController pushViewController:vc animated:YES];
To make SecondViewController like semitransparent, take screenshot of previous view controller, pass this image to SecondViewController and use it like background. You can apply this image to additional ImageView on your SecondViewController view or just call:
self.view.backgroundColor = [UIColor colorWithPatternImage:self.backgroundImage];

UINavigationController clear background color

That simple example but that don't work;
I have ViewController where inside on NavigationConroller, then I want to add new ViewConroller with its self navigation controller.
In main viewController:
CustomViewController *vc = [[CustomViewController alloc] init];
NewNavigationVC *nav = [[NewNavigationVC alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:NO completion:nil];
Two controllers has a background color clear, but still black color.
Navigation bar I can do clear, but not a view.
UPDATE:
if i change self.window.backroundColor to red for example, that work but not clear
UPDATE 2:
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];
and when I want to dealloc vc
[vc willMoveToParentViewController:nil];
[vc.view removeFromSuperview];
[vc removeFromParentViewController];
All work ok without navigation controller
A viewController's view's backgroundColor can't be clear (as in showing the previous viewController's view on the stack). Pushing or presenting a viewController will put the new viewController on the stack and hide the previous viewController completely.
If you want a clear backgroundColor on the view, you will need to either:
1) set the viewController as a childViewController of the previous viewController - then animate the transition yourself.
Or
2) transplant the viewController logic into the previous viewController and have a new uiview act as that view (you also need to animated the transition yourself).
The solution is as follows. For clear example we use tableViewController:
UITableViewController *modalVC = [UITableViewController new];
UINavigationController *modalNVC = [[UINavigationController alloc] initWithRootViewController:modalVC];
UIViewController *mainVC = [UIViewController new];
UINavigationController *mainNVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
modalVC.view.backgroundColor = UIColor.clearColor;
mainVC.view.backgroundColor = UIColor.redColor;
mainNVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[mainNVC presentViewController:modalNVC animated:YES completion:NULL];
The key feature is that you have to set modalPresentationStyle of presentingViewController to UIModalPresentationCurrentContext.
It works fine BUT without slide animation. You will get result immediately.
But you can still use "blood hack" to retain visual animation by successive presenting, dismissing and presenting again:
modalVC.view.backgroundColor = UIColor.clearColor;
mainVC.view.backgroundColor = UIColor.redColor;
[mainNVC presentViewController:modalNVC animated:YES completion:^{
[modalNVC dismissViewControllerAnimated:NO completion:^{
mainNVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[mainNVC presentViewController:modalNVC animated:NO completion:NULL];
}];
}];
You basically need to tell the navigation controller to:
navigation.modalPresentationStyle = .overCurrentContext
In other words:
A presentation style where the content is displayed over another view controller’s content.
and that's it.
You can also make sure that:
navigation.view.backgroundColor = .clear

presentModalViewController not displaying toolbar?

I have a viewcontroller I am trying to display as a UIModalPresentationFormSheet, however it does not show its UIToolbar. How can I make a toolbar show up on it?
ConnectionEditViewController * connectionEditViewController = [[ConnectionEditViewController alloc] initWithNibName:#"ConnectionEditViewController" bundle: nil];
connectionEditViewController.modalInPopover = YES;
connectionEditViewController.modalPresentationStyle = UIModalPresentationFormSheet;
connectionEditViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: connectionEditViewController animated: YES];
Create a UINavigationController instance, set the rootViewController to be your connectionEditViewController, then configure the modal properties for the UINavigationController and present that.
If this is on iPad, it should pop up the view with the Navigation Bar as a grey toolbar at the top.
Configure the navigationItem of the connectionEditViewController to configure the nav bar.

Resources