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
Related
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];
}
I came across a strange problem, i'am in landscape and present a VC
SomeViewController * vc = [[SomeViewController alloc] init];
[self.navigationController presentViewController:vc animated:YES completion:^{
}];
in the viewWillAppear the frame is incorrectly in portrait and in viewDidAppear it is correctly in landscape
but when i put it inside a UINavigationController
SomeViewController * vc = [[SomeViewController alloc] init];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController presentViewController:nc animated:YES completion:^{
}];
than in the viewWillAppear the frame is correctly landscape
Is this a bug? I'am i missing something? Why it is behaving like this?
I think it is by design, see this answer A: View frame changes between viewWillAppear: and viewDidAppear:.
By viewDidAppear the frame geometry is correct since it is now on-screen and added to the view hierarchy but I would actually recommend doing it in viewDidLayoutSubviews since that will be called when rotating orientations as well.
This only accours if you are presenting in a view controller that is managed by a navigation controller.
The reproduction steps are:
1 - Present a view controller using UIModalPresentationCurrentContext
self.definesPresentationContext = YES;
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
2 - Present a view controller over the top using the default full screen presentation style
ViewController* viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
3 - Dismiss the top presented view controller (the full screen one)
[self dismissViewControllerAnimated:YES completion:nil];
Now the problem is the 2nd view controller (presented using UIModalPresentationCurrentContext) disappears. Also it is impossible to present another view controller using UIModalPresentationCurrentContext, because the system thinks its still there.
I believe the issue is a bug in the framework. As mentioned it only occurs when the presenting in a view controller managed by a navigation controller. There is a nasty work around which uses the containment API. It creates a dummy view controller which views are presented from. The steps are:
1 - When presenting a view in context who's parent is a navigation controller, use a dummy view controller:
- (void)presentInContext
{
UIViewController* presentOnViewController = self;
if ([self.parentViewController isKindOfClass:[UINavigationController class]])
{
// Work around - Create an invisible view controller
presentOnViewController = [[DummyViewController alloc] init];
presentOnViewController.view.frame = self.view.frame;
// Containment API
[self addChildViewController:presentOnViewController];
[self.view addSubview:presentOnViewController.view];
[presentOnViewController didMoveToParentViewController:self];
presentOnViewController.definesPresentationContext = YES;
}
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
}
2 - When dismissing the view controller tidy up
- (void)dismissSelf
{
__weak UIViewController* presentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
// Remove the dummy view controller
if ([presentingViewController isKindOfClass:[DummyViewController class]])
{
[presentingViewController willMoveToParentViewController:nil];
[presentingViewController.view removeFromSuperview];
[presentingViewController removeFromParentViewController];
}
}];
}
Thats it... The fix is dirty, but does the trick with no visual flicker.
I would like to present a view controller full screen semi-transparently so that I still see the view underneath it. The following code presents the new view controller, but it replaces the current one. What is the best way to keep the original view controller visible? The view of the new view controller will have a semi-transparent black background.
NewViewController* newVC = [[NSClassFromString(#"NewViewController") alloc] initWithNibName:deviceNib bundle:nil];
newVC.modalPresentationStyle = UIModalPresentationFullScreen;
newVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:newVC animated:YES completion:NULL];
Present a semi-transparent View, not a view controller.
mySemiTransparentView.alpha = 0.0f;
[self.view addSubview:mySemiTransparentView];
mySemiTransparentView is your full-screen view. You can animate it into place:
[UIView beginAnimations:#"fadeIn" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.4f];
mySemiTransparentView.alpha = 0.5f;
[UIView commitAnimations];
You can present semi-transparent modal controller in the following way:
NewViewController* newVC = [[NSClassFromString(#"NewViewController") alloc] initWithNibName:deviceNib bundle:nil];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
newVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:newVC animated:YES completion:NULL];
Notice here that you have to set the constant UIModalPresentationCurrentContext to self.modalPresentationStyle, not to newVC.modalPresentationStyle
Furthermore, when you use UIModalTransitionStyleCrossDissolve the alpha of the newVC.view will be overridden during the transition, so if you want semi-transparent background you'll need to keep newVC.view's backgroundColor clear and just add another UIView as it's subview with semi-transparent backgroundColor
How can i go from a uitabbarcontroller to a viewcontroller in fullscreen, retaining all the data and views/tabs on the uitabbarcontroller?
tks
Did you want to hide the TabBar or show a new view, but once you're done with the new view have the TabBar be unaffected?
In the case of the latter try something like:
ViewController *vc = [[ViewController alloc] initWith...];
[self presentModalViewController:vc];
Add ViewController on TabBar in fullScreen then:
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController " bundle:nil];
[self presentViewController:vc animated:YES completion:NULL];
Now dissmiss that Modal Controller like this in any event( of button may be)
[self removeFromParentViewController];
After this your tabbar will as it is: