I've noticed that when I segue to a Naviagtion Controller, the navigation bar jumps slightly right after the flip animation completes.
It only does this when I use a flip horizontal segue, and not when I use the default slide up animation.
Here is a GIF I've made to illustrate the problem (sorry about the tiny size!):
http://i.imgflip.com/3ym0y.gif
Take note of the Nav bar with the title "Modal" -- notice it jumps down ~20 pixels after the animation.
The example above was created with a fresh project -- I have not subclased UINavigationController or UINavigationBar. Here is the storyboard, in case it helps:
http://i.imgur.com/qFlK2oB.jpg
What am I missing?
Add this to viewWillAppear in the controller you're presenting modally:
- (void)viewWillAppear:(BOOL)animated
{
// Workaround #1 for jumpy navbar
[self.navigationController.navigationBar.layer removeAllAnimations];
}
And when dismissing the controller (so in the same controller as above):
// Workaround #2 for jumpy navbar
[UIView transitionWithView:self.navigationController.view
duration:0.75
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:nil
completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
See https://stackoverflow.com/a/19265558/254603
Related
I have an app where the user can tap the screen and the navigation bar will fade out (I do this by setting the alpha to 0), but when the device is rotated the navigation bar reappears (I guess the alpha is reset to 1?). How can I prevent this behaviour?
The navigation bar stays hidden if I use [self.navigationController setNavigationBarHidden:YES animated:YES] instead, but that doesn't give me the fade effect I want.
Is there another way to do the fading effect?
Thanks
I assume you're animating out the navigation bar with the alpha? Why not hide the navigation bar once you're finished?
[UIView animateWithDuration:1.0
animations:^{
self.navigationController.navigationBar.alpha = 0;
} completion:^(BOOL finished) {
[self.navigationController setNavigationBarHidden:YES animated:NO];
}];
For testing purposes, I'm using the demo that's provided on Github. Search SASlideMenu to find the github repo.
Screenshot 1:
Screenshot 2:
Ok, so I'm using SASlideMenu in my app and here is what I want to do:
In a nutshell, I want to go from the blue page (the one with the button that says "Press me to get to screenshot 1") with a button press, to another one of the options in the menu. I've added the option to get to screenshot 1 to the menu and clicking on this it takes me to that view WITH the navigation bar at the top.
The problem is, when I press the "Press me to get to Screenshot 1" button, it takes me to the screenshot 1 page without the navigation bar at the top of the view. I've used a modal segue to link the button to the view shown in screenshot 1.
I want this view to have the navigation bar just like in the blue screenshot.
Is this possible?
I hope this is clear. If you need any clarification, feel free to ask.
You can't reuse that top bar in different view's as it is here
But following may be solution
1. If top bar is navigation bar then use push segue instead of modal and setup buttons again
2. If you want to use same top bar, just mimic modal view presentation by writing custom segue
//code to mimic modal presetation will be similar to this
- (void)perform
{
//add as subview with origin y at bottom -50
CGRect frame = [[self destinationViewController] view].frame;
frame.origin.y = [[self sourceViewController] view].frame.size.height -50;
[[self destinationViewController] view].frame = frame;
[[[self sourceViewController] view] addSubview:[[self destinationViewController] view]];
//animate frame with new y postion
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = [[self destinationViewController] view].frame;
frame.origin.y =[[self sourceViewController] view].frame.origin.y - SOME_OFFSET_VALUE_TO_SHOW_TOP_BAR;
[[self destinationViewController] view].frame = frame;
}];
}
3. If you dont wan't modal segue, then just use custom segue like in 2. and add your view (white one) as subview in area below navigation bar
I want to present a view controller with a slightly transparent background modally over the current view, such that the first view is slightly visible under the modal view.
I set the alpha value of the modal view controller and set the modalPresentationStyle to UIModalPresentationCurrentContext, as suggested in another post.
The result is that the view background is transparent when animating up, but when view controller is in place it changes to opaque black. It goes back to being transparent while animating the dismissal.
How can I get it to be transparent when active ?
I have tested in iOS 6 and 7. The code I am using follows:
MyModalViewController *viewController = [[MyModalViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navController setNavigationBarHidden:YES];
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self.navigationController presentViewController:navController animated:YES completion:NULL];
iOS 8 added a new modal presentation style specifically for this purpose:
presentedViewController.modalPresentationStyle = UIModalPresentationOverFullScreen
From the spec:
UIModalPresentationOverFullScreen
A view presentation style in which the presented view covers the screen. The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.
If you are targeting ios 8 and above you can set the modal presentation style to "over current context" and you are done.
If ios 7 and below, you would have to create a custom transition style so that the presenting screen doesn't go blank after transition. That is rather complicated.
The solution I present offers a lot of flexibility: make a screenshot before showing the modal dialog and set that as the background image for the application window. By default, that background is black (that is what you see when the back view controller dissapears). Change the background to the screenshot of the app. Make the screenshot in the viewWillAppear or viewDidLoad method of your transparent view. This works even with push segues, not only modal dialogs, but you should avoid animations. In general, avoid animations which affect the position of the background view because those will make it seem like it snaps back into place when transition finishes. It is a good idea to reset the background to its previous black image on viewDidDissapear to avoid unwanted effects.
You can maintain a stack of such background images and you can do multiple "transparent" push seques. Or have some complex/deep menu which appears on top of some main screen. For these many reasons I think this solution is better than rolling your own transitioning code. It is more flexible and easier to implement, and you don't have to deal with the animations yourself.
The reason that the BG view controllers disappear after a modal is shown is that the default transition in iOS 7 removes the BG view after animation completed. If you define your own transition and you set your BG view not to be removed (just changing its alpha) then you will have the transparent modal view.
Same problem occured to me. I have solved it by looking at the following url about a custom alert controller. I managed to get it working even with a UINavigationController.
Swift
let viewController = UIViewController()
viewController.providesPresentationContextTransitionStyle = true
viewController.definesPresentationContext = true
viewController.modalPresentationStyle = .overCurrentContext
viewController.modalTransitionStyle = .crossDissolve
DispatchQueue.main.async {
self.navigationController?.present(viewController, animated: true, completion: nil)
}
Objective C
UIViewController *viewController = [UIViewController new];
viewController.providesPresentationContextTransitionStyle = true;
viewController.definesPresentationContext = true;
viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController presentViewController:viewController animated:true completion:nil];
});
Here is a solution.
Create your presenting view controller. Add a backView to this view controller's main view. Name this as backView.
In SecondViewController.m
-(void)viewDidLoad
{
// Make the main view's background clear, the second view's background transparent.
self.view.backgroundColor = [UIColor clearColor];
UIView* backView = [[UIView alloc] initWithFrame:self.view.frame];
backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
[self.view addSubview:backView];
}
Now you have a view controller with half transparent background. You can add anything you want to the self.view , the rest will be half transparent.
After that, in FirstViewController.m
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:secondViewController animated:YES completion:nil];
My solution is this:
Create a custom transparent overlay UIView that comes over any view, navigationbar and tabbbar.
-In the navigation controller (or tabbar controller) that your view controller is embedded in I create a custom view with it's frame equal to the frame of the navigation controller's view.
-Then I set it offscreen by setting it's origin.y to navigationController.view.height
-Then I create 2 functions -(void)showOverlay and -(void)hideOverlay that animate the overlay view on and off screen:
- (void)hideOverlay{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGRect frm = self.helpView.frame;//helpView is my overlay
frm.origin.y = self.offscreenOffset; //this is an Y offscreen usually self.view.height
self.helpView.frame = frm;
[UIView commitAnimations];
}
- (void)showOverlay{
[self.view bringSubviewToFront:self.helpView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGRect frm = self.helpView.frame;
frm.origin.y = self.onscreenOffset;
self.helpView.frame = frm;
[UIView commitAnimations];
}
-In my view controller I can just call
[(MyCustomNavCtrl *)self.navigationController showOverlay];
[(MyCustomNavCtrl *)self.navigationController hideOverlay];
And that's about it.
FYI: The syntax is now:
childVC.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen
Why don't you try setting this in AppDelegate
self.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
then changing the alpha on the view being presented
my app consists of a table view controller and a view controller. when i press a cell in the table view, the toolbar at that view slips with animation down outside the screen and when i'm in the view controller and press back, the toolbar bar slips upwards to it's original position. My problem is, i figured out a bug that when i'm in the view controller and press the home button to exit the app and then come back. the app resumes where i left but when i go back to the table view, the toolbar shifts upwards beyond it's original position. the sliding of the toolbar works fine when i'm in the app before exiting. so there's like something is being called to reset the toolbar to it's origin and thus adding the additional y-axis point to shift more upwards. does anybody know what are those methods?
Code:
i have this in the viewWillAppear method of the view controller:
[UIView animateWithDuration:0.7 animations:^{
self.navigationController.toolbar.center = CGPointMake(self.navigationController.toolbar.center.x, self.navigationController.toolbar.center.y + self.navigationController.toolbar.frame.size.height);
} completion:^(BOOL finished){
self.navigationController.toolbar.hidden = YES;
}];
and in the same view when it needs to disappear i added this in the viewWillDisappear:
[[self.navigationController toolbar] setHidden:NO];
[UIView animateWithDuration:1 animations:^{
self.navigationController.toolbar.center = CGPointMake(self.navigationController.toolbar.center.x, self.navigationController.toolbar.center.y - self.navigationController.toolbar.frame.size.height);
} completion:^(BOOL finished){
}];
i tried this as another way to animate the hiding of the toolbar but there is no animation:
- (void) viewWillAppear:(BOOL)animated
{
[self.picker setHidden:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
}
- (void) viewWillDisappear:(BOOL)animated
{
[self.navigationController setToolbarHidden:NO animated:YES];
[self.course setValue:self.nameTextField.text forKey:#"courseName"];
[self.course setValue:[NSNumber numberWithInt:[self.creditsTextfield.text integerValue]] forKey:#"courseCredits"];
[self.course setValue:[NSNumber numberWithInt:[self.chaptersTextfield.text integerValue]] forKey:#"courseChapters"];
[self.course setValue:self.gradeTextfield.text forKey:#"courseGrade"];
}
For one thing, you should use frame instead of center, but replace the viewWillDisappear animation line with this:
self.navigationController.toolbar.frame = CGRectMake(0,0,self.navigationController.toolbar.frame.size.width, self.navigationController.toolbar.frame.size.height);
and tell me what happens...
That should fix your problem.
Since you are using the toolbar belonging to a UINavigationController (and not a standalone UIToolbar instantiated and added to a UIView by your own controller), it's better to use the methods that UINavigationController exposes, because you don't know how it manages and move its UIToolbar.
Try to put just this in viewWillAppear:, instead of the entire animation block
[self.navigationController setToolbarHidden:NO animated:YES];
and this in viewWillDisappear:
[self.navigationController setToolbarHidden:YES animated:YES];
i fixed it! i aded the following lines of code in viewWillDisappear:
self.navigationController.toolbar.center = CGPointMake(self.navigationController.toolbar.center.x, 458);
self.navigationController.toolbar.center = CGPointMake(self.navigationController.toolbar.center.x, self.navigationController.toolbar.center.y + self.navigationController.toolbar.frame.size.height);
since the problem seems that when the app goes background then foreground the toolbar resets to it's original position and thus after navigating back to the table view the toolbar is shifted beyond it's original position. therefore the first line i added resets the toolbar to it's original position while still hidden then shift it down. after that the block of animation is done. i did this so that the animation works on the following cases:
1. the user enters the detail view from the table view then goes back to the tableview without exiting the app.
2. the user enters the detail view from the tableview then exits the app and then resume the app and goes back to the table view.
Here's the situation:
I am making an app for iPad w/ iOS 6 using Autolayout along with UINavigationController. What I am trying to do is:
Segue from one view controller to the next with a standard push segue.
When I arrive at the new view controller, hide the nav bar with animation.
As the nav bar hides, I want my view to not shift at all. In fact, I want my view to effectively be drawn underneath the nav bar from the beginning, so I'm left with no shifting or movement of content and no black bars. For reference, this is what happens in the Amazon Kindle app when you go into a book.
With my current code, the contents of my view shift up to fill in the void left by the UINavigationBar.
I've tried force-setting the frame of my UIViewController's view and my UINavigationController's view to the entire iPad screen in the viewWillAppear method of my viewcontroller but no dice. I've experimented w/ Constraints in Autolayout but that also didn't get me to where I wanted to go.
Any help you can give would be great!
Try following before animating the navigation bar:
self.navigationController.navigationBar.alpha = 0.99f;
I didn't try this but this should work.
Looks like you need to add custom navigation bar in your new view and animate it to disappear.
I think, hiding original Navigation bar of Navigation Controller without shifting the view is not possible.
Rather add UINavigationBar to xib file, bind it to IBOutlet uiNavigationBar and try following code
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
CGRect f = self.uiNavigationBar.frame;
f.origin = CGPointMake(f.origin.x, f.origin.y - 44);
self.uiNavigationBar.frame = f;
} completion:^(BOOL finished) {
NSLog(#"done");
}];
}