tab bar hidden after rotation - ios

I haven't found anything corresponding to my situation so far...
FYI, I'm developing for iOS 5, using a storyboard.
I have a tab bar controller with 2 views in it (let's call them tab 1 and tab 2). I also have a separate landscape view, with no tab bar, which is used any time the device rotates during the application use. I use a segue launched manually in shouldAutorotateToInterfaceOrientation to switch to and from this view. I also use a NSString in the landscape view to know which tab I am coming from, to go back to correct one when I go back to portrait. So far, this works fine. I can go to and from landscape mode exactly the way I want.
My problem is :
When I launch the app, in portrait, I see the tab bar. If I go to landscape, it disappears. This is fine, that's what I did in my storyboard. But when I go back to portrait, the tab bar does not come back ! That's the problem.
Edit : code calling the rotation
I stopped using shouldAutorotateToInterfaceOrientation to rotate because it was conflicting with the custom segues. The problem with the tab bar was here before, so this is not the issue. I use didRotate instead.
Here is the code from FirstViewController.m (same code in SecondViewController.m, changing my segue identifier) :
-(void)didRotate:(NSNotification *)notification
{
UIInterfaceOrientation newOrientation = [[UIDevice currentDevice] orientation];
if ((newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight))
{
[self performSegueWithIdentifier: #"Page1ToLandscapeSegue" sender: self];
}
}
And from LandscapeViewController.m (previousView is a NSString, set before going to landscape, so I know which view I'm coming from) :
-(void)didRotate:(NSNotification *)notification
{
UIInterfaceOrientation newOrientation = [[UIDevice currentDevice] orientation];
if (newOrientation == UIInterfaceOrientationPortrait)
{
if ([previousView isEqualToString: #"View1"]) {
[self performSegueWithIdentifier: #"LandscapeToPage1Segue"
sender: self];
}
else if ([previousView isEqualToString: #"View2"]) {
[self performSegueWithIdentifier: #"LandscapeToPage2Segue"
sender: self];
}
}
}

Looking at your comments I'm thinking that your tab bar is disappearing because you're seguing from a view controller that is not embedded in a tab bar controller (this being your landscape view view controller), I'd suggest the following:
1) It seems complicated to setup segues to go back to the previous view, not to mention that you're creating more views/controllers and adding them to the stack, so discard the segues that go back to your original views.
2) Make the segues to the landscape view be modal, that way you won't have the tab bar show up when you segue to them, if you use push it will be embedded in the tab bar controller.
3) Since the landscape view would be a modal view, simply call this method in your rotate code in the landscapes view controller:
[[self presentingViewController] dismissModalViewControllerAnimated:YES];
This will push the view off the stack and go back to the view it came from.

Related

iOS 8 view isn't removed from stack after dismissViewControllerAnimated is called

This is my first post on SO, but I am very familiar with the site. I currently have an app that was working correctly until a recent iOS update. Here is the situation:
I have a PageViewController with a page that holds a TableViewController. When the phone orientation is changed from portrait to landscape, a modal view is created and presented to the user, using a modal segue. If the user then changes the orientation of the phone back to portrait, the app crashes.
I have spent many long hours trying to do work arounds and find/create solutions, but no luck. What I have discovered is "dismissViewControllerAnimated" is being called and the modal view is being deallocated. However, I have a print statement in at the beginning of my "orientationChanged" function. It prints once after the first time from landscape to portrait. But if the user rotates the phone to landscape again and back to portrait, "orientationChanged" will now be called twice, as though the view controller was never removed from the stack. If the user proceeds to do this 10 times, for example, on the 10th time "orientationChanged" will be called 10 times in a row.
This just doesn't make any sense to me. If the modal view is being deallocated, then how is this possible? Is there a copy of the modal view being stored somewhere? I tried to change the modal segue to be a push segue, but no luck. It would result in the same thing. Using the push segue and navigation bar that comes with it, I was able to navigate backwards through the view stack. The stack had duplicate copies of my desired modal view, like they were never removed from the stack, and at the bottom of the stack was my TableViewController.
Here is my orientationChanged function:
- (void)orientationChanged:(NSNotification *)notification
{
NSLog(popModalView ? #"Yes" : #"No");
NSLog(#"Index:%lu",(unsigned long)self.pageIndex);
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && popModalView == NO)
{
[self performSegueWithIdentifier:#"DisplayGraphNumberOfJobs" sender:self];
popModalView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) && popModalView == YES)
{
NSLog(#"dismiss");
[self dismissViewControllerAnimated:YES completion:nil];
popModalView = NO;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"DisplayGraphNumberOfJobs"])
{
GraphJobTotalsVC *graphJobs = [segue destinationViewController];
//initialize graphJobs...
}
}
As a side note, my PageViewController has 3 pages, each with the same problem as this one. So solving one page should solve them all, hopefully. I have looked all over apple's documentation, but no luck. I have looked at countless other SO answers as well. I hope someone can help me. Thank you, in advance.

Unwind segue doesn't dismiss adaptive popover presentation when not modal

Update for iOS 9 beta: Apple may have fixed this for iOS 9. If you work(ed) around this issue for iOS 8, make sure it also works correctly on iOS 9.
In storyboard, I've created a popover presentation segue to present a navigation and view controller from a button, as well as creating an unwind segue.
In portrait orientation, the modal (fullscreen) presentation is unwound/dismissed, as expected.
In landscape orientation, the unwind segue also gets called, however the popover presentation is not automatically dismissed.
Did I miss hooking something up? Do I have to dismiss the popover presentation myself?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)__unused sender
{
if ([[segue identifier] isEqualToString:#"showSelectBookChapter"])
{
UINavigationController *navigationController = segue.destinationViewController;
if ([navigationController.topViewController isKindOfClass:[BIBLESelectViewController class]])
{
BIBLESelectViewController *selectViewController = (BIBLESelectViewController *)navigationController.topViewController;
selectViewController.initialBookChapterVerse = self.bookChapterVerse;
}
}
}
- (IBAction)unwindToBIBLEChapterViewController:(UIStoryboardSegue *)segue
{
if ([segue.identifier isEqualToString:#"unwindToBIBLEChapterViewController"]) {
if ([segue.sourceViewController isKindOfClass:[BIBLESelectViewController class]])
{
BIBLESelectViewController *sourceViewController = (BIBLESelectViewController *)segue.sourceViewController;
self.bookChapterVerse = sourceViewController.selectedBookChapterVerse;
[self.tableView reloadData];
}
}
}
Update:
After looking at gabbler's sample code, I've narrowed the problem down to popover dismissing fine in a Single View Application, but not in a Master-Detail Application.
Update 2:
Here's what the hierarchy looks like (omitting navigation controllers for simplicity's sake), in answer to the question Luis asked:
Split view controller
Master view controller
Detail view controller
Chapter view controller (modal page sheet)
Select view controller (the problematic popover that unwinds to chapter view controller, but doesn't dismiss)
As I mentioned in the previous update, I created an new master/detail template, and simply presented a popover directly from (a button in) the detail view. It won't dismiss.
I ran into this problem too. I present a View Controller modally (as a form sheet), from the Master View Controller (UISplitViewController). The problem only occurred on the iPad (probably the iPhone 6+ in landscape mode too, but I didn't check it). I ended up doing the following in my unwind action method (using Swift), and it works good.
if !segue.sourceViewController.isBeingDismissed() {
segue.sourceViewController.dismissViewControllerAnimated(true, completion: nil)
}
If you segue as a popover from a view controller embedded in a navigation controller, the corresponding unwind fails to dismiss the popover.
It's a bug in -[UINavigationController segueForUnwindingToViewController:fromViewController:identifier]. The embedding navigation controller is supposed to supply a segue that will dismiss the popover but it doesn't. The fix then is to override this and supply a working segue, which we can get from the embedded view controller.
Here's a partial solution that will only handle unwinding to the top view controller of the navigation stack:
#implementation MyNavigationController
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController
fromViewController:(UIViewController *)fromViewController
identifier:(NSString *)identifier
{
if (toViewController == self.topViewController && fromViewController.presentingViewController == self)
return [toViewController segueForUnwindingToViewController:toViewController
fromViewController:fromViewController
identifier:identifier];
else
return [super segueForUnwindingToViewController:toViewController
fromViewController:fromViewController
identifier:identifier];
}
#end
It works on iOS 8 for both landscape/portrait iPad and landscape/portrait iPhone. The logic should be robust enough to survive on iOS 9.
It is/must be a behavior of the popOver segue, in normal situations or regularly we need that the popOver keeps in view, if the segue show something important is annoying that we lost that information just because we rotate the device, I guess that that is the reason of that native behavior. So if we want for it to dismiss automaticly we have to make that behaivor by our own, this works:
in the method - (void)viewDidLoadin the detailViewController.m add this:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
then create this method:
- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = note.object;
//CGRect rect = [[self view] frame];
switch(device.orientation)
{
default:
[self dismissViewControllerAnimated:YES completion:nil];
break; }}
You said that in a single view happens what you want, but I've never seen that behavior when I used popOvers.
mbeaty's fix is great but as others have pointed out, this bug seems to know be fixed in iOS 9 and it also doesn't work well for universal device design. I have adapted his answer to handle both situations. Here is the code:
#IBAction func yourUnwindSegue(segue: UIStoryboardSegue) {
if #available(iOS 9, *) {
return // bug fixed in iOS 9, just return and let it work correctly
}
// do the fix for iOS 8 bug
// access your SplitViewController somehow, this is one example
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let splitVC = appDelegate.window!.rootViewController as! YourSplitViewController
// if the source isn't being dismissed and the splitView isn't
// collapsed (ie both windows are showing), do the hack to
// force it to dismiss
if !segue.sourceViewController.isBeingDismissed() && splitVC.collapsed == false {
segue.sourceViewController.dismissViewControllerAnimated(true, completion: nil)
}
}
This first checks if iOS 9 is running and just exit as the bug seems to be fixed. This will prevent the multiple views getting dismissed issue. Also to make sure this fix is only done when the splitView is showing two windows (to make it happen only on iPads and iPhone 6 Plus in landscape as well as future devices) I added the check to make sure it is not collapsed.
I have not exhaustively check this but it seems to work. Also not that my app is set for a min of iOS 7, I don't know if this bug existed then so you may need to look into that if you support below iOS 8.

keyboard not appearing after return from ModalViewController

I have a UIViewController then with this code I present a modal view controller for entering the password.
[self performSegueWithIdentifier:#"SeguePassword" sender:self];
in Storyboard:
Storyboard Segue
Identifier = SeguePassword
Style = Modal
Transition = default
Animates = not checked
When I click "Cancel" on the modal which has the following code:
[self dismissViewControllerAnimated:NO completion:^{}];
Now when I get back the keyboard is hidden. I have code to show a toolbar with a button called hide. And I see that, but not the keyboard.
Does anyone have any ideas or directives they have taken to fix an issue like this? It seems it recently started after converting changes for iOS 7.
My solution:
I found this because after I clicked the but on an alert box a few times out of frustration I saw the keyboard in the wrong orientation. I.e.., a landscape keyboard when the app is portrait only on iPhone.
Before:
- (NSUInteger) supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
} else {
**return (UIInterfaceOrientationPortrait);**
}
}
After:
- (NSUInteger) supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
} else {
**return (UIInterfaceOrientationMaskPortrait);**
}
}
I changed the Portrait to use the Mask integer and everything started working again.
The keyboard is only visible if some object is the firstResponder. Look at the documentation for the UIResponder class.
If your modal view controller uses the keyboard, which it sounds like it does, it takes first responder status and whatever had it on your original screen loses it. If you want to return to the same state from the modal view controller, with some object having input focus -- i.e. being the firstResponder -- with the keyboard visible you will have to have write code to make that happen. In particular, your original view controller has to know when the modal view controller terminates so that it can make your input object, textField or textView or whatever, be the first responder with the statement:
[objectIWantToBeFirstResponder becomeFirstResponder];
Now how do you know when the modal view controller is done?
There are a couple obvious techniques.
If you want to use a segue then the modal view controller will have to
explicitly let the presenting view controller know that it is
terminating. You will probably want to define a protocol in the
modal view controller and have the presenting view controller adopt
it. When the modal view controller wants to exit it first tells the presenting
view controller.
Since you are manually triggering the segue perhaps
you're willing to present the modal view controller in code. If so then
you can use a completion block:
ModalViewControllerClass *vc = [[ModalViewControllerClass alloc] init];
// whatever else you need to do to the vc
[self presentViewController:vc animated:YESorNO completion:^{
// This code gets executed when the presented view controller exits
[objectIWantToBeFirstResponder becomeFirstResponder];
}];

UIModalPresentationCurrentContext and willRotateToInterfaceOrientation

I have a modal view controller (let's call it the popup view controller) presented over my main view controller. At some point, I need so see the main view controller behind the popup. Therefore, I set the modalPresentationStyle property to UIModalPresentationCurrentContext before presenting the popup view controller.
So what happens is that when the device orientation changes while the popup view controller is modally presented, the willRotateToInterfaceOrientation: and didRotateFromInterfaceOrientation: methods are not called. supportedInterfaceOrientations is called amd returns the good value. The rotation is enabled, the supported orientations are correctly set. The popup conIt actually works if I change 'modalPresentationStyle' to the default value, everything works fine, except that obviously I do not see the main view controller behind.
I should add that the main view controller only supports portait, while the popup above it supports all orientations.
On an iOS 5.1 device, the willRotate and didRotate methods are correctly called. It is only on the iOS 6 device that they are not.
Did anybody encountered a similar issue or already needed to display a transparent multi-orientation view controller modally above a single orientation view controller?
Do this...
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
UIDevice* thisDevice = [UIDevice currentDevice];
if (thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
return true;
}
else
{
return interfaceOrientation == UIDeviceOrientationLandscapeLeft;
}
}

how to allow autorotation for child vc of vc in tabbar?

I have a tab bar view controller, which contains some navigation controllers each with a root view controller. Everything should only render in portrait. All's well. One of the child view controllers pushes another view controller using it's navigation vc, and that one I'd like to allow rotation to landscape on.
Tabbar vc
nav vc/vc1
nav vc/vc2
nav vc/vc3
pushes new vc4 <- ONLY this one should autorotate. Further more, if you're in landscape mode in this vc and hit 'back', it goes back in portrait.
The app 'line' does this - the actual 'chat' window can autorotate.
Please give me the codes! ;) I'm hoping it doesn't require messing with the transforms...
To support autorotation on your UITabBarController application, you need to subclass the view controller rotation call on your tabBarController and ask the currently presented view controller if he supports the new orientation. In your specific case, vc4 will be asked if he supports the orientation change, so the tab bar can rotate when asked.
Take at look at this question: tabBarController and navigationControllers in landscape mode, episode II
#Colin In ur Tabbar viewcontroller,restrict it to potrait like
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
and in ur new viewcontroller(vc4),restrict it to landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight) );
}
Hope it helps.

Resources