Rotation not animating for view after tabViewController setSelectedIndex - ios

The rotation animation occurs for the status bar (which has the clock and the battery icon), but the view itself just changes size, it doesn't do the page flip animation. In the gif (below), I did a few screencaptures of the rotation animation in slow motion. You can see the clock and battery icon rotate into the view, even though the content just scales.
http://imgur.com/gallery/Q3OXCIH
I found some similar, but not quite the same posts:
iOS Device Rotation Instant Snap rather than animation
iOS 9 Orientation Auto-Rotation Animation Not Working, But Always on Main Thread
This is somewhat repeatable- at first, the rotation occurs correctly, but after I programmatically change the tab view controller index, it can trigger. After it triggers, the rotation animation does not occur for the view until after I reset the app.
Code where I change the tab view controller and then change it back:
[appDelegate.tabBarController setSelectedIndex:0];
...code to operate on the code at index 0...
[appDelegate.tabBarController setSelectedIndex:2];
To emphasize- it DOES animate the rotation correctly when I first run the app. Behaves the same in simulator and in hardware. IOS9. Xcode 7.1.1.
Anyone know why a viewcontroller's content would stop animating during rotation?
edit-
To answer fragilecat's questions:
1) I am set up to use the rotation functions, as described in https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/clm/UIViewController/attemptRotationToDeviceOrientation
I have implemented shouldAutorotateToInterfaceOrientation and shouldAutorotate and supportedInterfaceOrientations. The supportedInterfaceOrientations gets called once, when the viewcontroller loads. shouldAutorotateToInterfaceOrientation and shouldAutorotate are apparently never called.
2) I am receiving size change messages via viewWillTransitionToSize- this is ios9 so there aren't any rotation messages. willTransitionToTraitCollection is apparently never called, though it is overriden. I am calling the super for both.
3) I am not using viewWillLayoutSubviews() or viewDidLayoutSubviews(). I am only overriding viewDidLoad and viewWillAppear. These do not affect rotation.
4) I am not dynamically changing rotation methods.
What I did notice, is that rotation works at first, but then fails (doesn't rotate but just scales), after I change the tabBarViewController selectedViewController programmatically after the use clicks "ok" to an alertview. I haven't figured out why yet, but it is repeatably after that event.
Sequence of the bug:
Works fine, rotates ok.
User hits "ok" to an alertview
I programmatically call [tabBarViewController setSelectedIndex:0]
I call some functions on the viewcontroller at index 0.
I programmatically call [tabBarViewController setSelectedIndex:2] (back to the original)
rotation now does not occur reliably

Anyone know why a viewcontroller's content would stop animating during rotation?
The child view controller's rotation method's are configure incorrectly.
Your child view controller's are not receiving rotation messages.
You have custom code in the view controller's layout methods.
Your code is dynamically altering rotation/layout/animation methods.
I am looking for direction on what kind of bugs can produce this type of error.
This is some what hard to answer with out seeing your code, but here are the steps I would take in trouble shooting this issue.
Confirm that your child view controllers are correctly setup for rotation.
UIViewController Rotation Methods
Confirm that your child view controller's are receiving the following messages so that you can rule out this as the issue as this is how rotation is handled in iOS 9.
func willTransitionToTraitCollection(_ newCollection: UITraitCollection,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
func viewWillTransitionToSize(_ size: CGSize,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
If you are overriding these methods else where you should make sure you call the super's version, see UIContentContainer for details.
Comment out any custom code that you might have in viewWillLayoutSubviews() implementation of your view controllers. Also you can check your frames between viewWillLayoutSubviews() and viewDidLayoutSubviews().
EDIT
You need to execute the tab change on the main thread. If I am correct you are using the delegate of UIAlertView? I don't think your on the main thread went you make your call!
dispatch_async(dispatch_get_main_queue(), ^{
[appDelegate.tabBarController setSelectedIndex:0];
});

Related

iOS8 - prevent rotation on presenting viewController

We have a MainViewController with a tableView, and it presents a new modalViewController.
The MainViewController is restricted to portrait only, and the modalViewController can rotate.
The problem is in iOS8, that when the modalViewController rotates, the callback method of rotation in iOS8 in MainViewcontroller is called - - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
Thus, the UITableView is getting its data reloaded, which is a behaviour we don't want.
Can we prevent this feature of iOS 8, and not rotate the presenting UIViewController?
So after long days of searching and investigating, I finally came up with a possible solution.
First of all, I can use navigation controller and push the viewController instead of presenting it, but it breaks my code and just isn't so true.
The second thing I can do is not setting constraints. I still can use autolayout, but if I don't set constraints, and let the default constraints to be set, the tableView doesn't get reloaded. of course this is also isn't very smart thing to do, as I have many elements in my viewController.
Finally, I figured out that I can show this "modal" viewController in another UIWindow. I create UIWindow and set the modalViewController as its rootViewController.
I put some example project in git:
https://github.com/OrenRosen/ModalInWindow
Hope it will be helpful.
I did something similar with a navigation controller, that wouldn't rotate unless the top pushed controller does rotate.
In your case check if the main controller is presenting another controller. If it isn't then just reject rotation, otherwise return whatever the presented controller returns for the rotation method.
As for your table view, it shouldn't get reloaded because of rotations.
In iOS 8 the view that rotates when you change the device orientation is the first view added to the UIWindow. So, if you save a reference to it in your presentedController, you can overwrite the shouldAutorotate and supportedInterfaceOrientations values.

How to check orientation of interface prior to viewDidAppear

I have a LoginViewController that you can navigate back and forth from the MainMenuViewController. I am adjusting subviews' positioning and size programmatically using willAnimateRotationToInterfaceOrientation
This works great. The problem I am having is that I need to check the orientation right when the ViewController is loaded, in case it is loaded and you are in landscape orientation, the same changes I make in willAnimateRotationToInterfaceOrientation will be made.
The problem is, the earliest point I can get self.interfaceOrientation is in the viewDidAppear method, and this causes the user to see the original sized/positioned subviews for a split second before it transitions to the landscape-appropriate sizes/positions. I tried in viewDidLoad and viewWillAppear and neither of these work because (what I believe), self.interfaceOrientation is still NULL at this moment. How can I work around this and get the changes to be made prior to the user viewing the page (viewDidAppear)?
Any help would be greatly appreciated. Thank you!
It seems like you are looking for this method :
- (void)viewWillLayoutSubviews
{
}
https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/index.html
"The viewWillLayoutSubviews method is also called after the view is resized and positioned by its parent."

UITabBarController's tabs' rotation and willRotateToInterfaceOrientation propagartion

My app has four tabs: A, B, C and D. Their UIViewController are managed by UITabBarController. The app supports rotation, and so each view controller returns YES to shouldAutorotateToInterfaceOrientation.
Using springs and struts, most of the rotation is done automatically by iOS. However, tab A also requires further positioning, and it is done in its VC's willRotateToInterfaceOrientation method.
When the VC for tab A is selected and the screen is rotated, that VC receives a willRotateToInterfaceOrientation message (propagated by iOS from UITabBarController), and the resulting rotation is correct.
However, when the selected tab is B and the screen is rotated, A's willRotateToInterfaceOrientation is not called. Makes sense. But if I then select tab A, I get only the results of applying its springs and struts, without the post-processing done by its willRotateToInterfaceOrientation.
After struggling with this for a while, and after failing to find a solution online, I came up with the following. I subclassed UITabBarController and in its willRotateToInterfaceOrientation I call all the VCs' willRotateToInterfaceOrientation regardless of which one is the selectedViewController:
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (self.viewControllers != nil) {
for (UIViewController *v in self.viewControllers)
[v willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}
It works, but it looks like a hack, and my question is whether I was doing the right thing. Is there a way to tell iOS to always call a VC's willRotateToInterfaceOrientation before displaying it for the first time after a screen rotation?
The best way to handle custom layout is by subclassing UIView and overriding the layoutSubviews method. The system sends layoutSubviews to a view whenever its size is changed (and at other times). So when your view A is about to appear on screen with a different size (because the interface was rotated while view B was on screen), the system sends view A a layoutSubviews message, even though it doesn't send view controller A a willRotateToInterfaceOrientation: message.
If you are targeting iOS 5.0 or later, you can override the viewDidLayoutSubviews method of your UIViewController subclass and do your layout there, instead of subclassing UIView. I prefer to do it in my view's layoutSubviews, to keep my view-specific logic separate from my control logic.
It's also a bad idea to do layout in willRotateToInterfaceOrientation: because the system sends that message before actually changing the size of the view, and before the rotation animation block. It sends the willAnimateRotationToInterfaceOrientation:duration:, layoutSubviews, and viewDidLayoutSubviews messages inside the rotation animation block, so the repositioning of your subviews will be animated if the view is on screen during the rotation.

When dismissing a fullscreen modal the viewWillLayoutSubviews method does not get called

I am trying to track down a problem where the viewWillLayoutSubviews (and viewDidLayoutSubviews) method do not get called after dismising a controller displayed using -
[self presentModalViewController:controller animated:YES];
and dismissing with
[self dismissModalViewControllerAnimated:YES];.
This view controller is displayed on top of a UISplitViewController as the result of a button being pressed in the detail area. When I rotate the device, without the modal up, I do get the viewWillLayoutSubviews callback. However, the problem is, when I rotate during the presentation of the model, it does not update the views correctly and recalculate the view bounds after dismissing it. According to the IOS 5 release notes I should get a viewDidLayoutSubviews after dismissing the modal view controller.
For comparison, I created a bare bones app with none of my other code in it and it works as documented, it will call viewWillLayoutSubviews after the modal is dismissed.
I have been going over and over my real app code and can't find anything wrong. I am looking for suggestions for things to do help figure this out. Why would the callback work when rotating but not work when the modal is dismissed? Could it be something with my view hierarchy?
Thanks for any help!
Try using the delegate method viewWillAppear instead of viewWillLayoutSubviews. The WillLayoutSubviews is only called when the view's bounds change (which happens when you rotate the device).

iOS: Orientation check rotation on ViewDidAppear

Background: I want to make sure my viewControllers rotate properly when it appears. My viewControllers have excellent codes managing the rotation and orientation when it is visible.
Problem: Given two viewControllers in a NavigationController, viewC1 and viewC2. I did the following:
1.) Set rootViewController to viewC1
2.) Push viewC2 into the NavigationController
3.) Rotate
4.) Pop viewC2
5.) viewC1 is still stucked in the old orientation look (as in the transformation code in willAnimateRotationToInterfaceOrientation was not called) with the new orientation.
What can I do to ensure viewC1 call willAnimateRotationToInterfaceOrientation to reconstruct itself to look correctly in the new rotation?
Additional info:
This is all code (no storyboard/xib). I have shouldAutorotateToInterfaceOrientation return YES on all the views. I use willAnimateRotationToInterfaceOrientation to manage all my rotation.
Oh, and please no hacks. For example, copy the code from rotation then check the rotation mannually and manage it in viewDidAppear.
Think about the name of the method, and what you're trying to achieve.
willAnimateRotationToInterfaceOrientation indicates that the view controlled by the view controller is about to animate to a particular orientation. If your view is in the middle of a navigation stack, then it is not being displayed on screen. To animate something that isn't on screen is costly and ultimately worthless. So, that particular method is out of the question, but the problem that remains is there isn't anything else more appropriate in UIKit. The reason is to rotate something (even if not animated) when it's offscreen is worthless cost. It's the responsibility of the developer to handle a change in orientation when the view appears ("transformation on demand" as you will).
You say you don't want hacks, but the method you've described as a hack is probably the most appropriate thing to do. Having a generic method named something like
-(void) updateLayoutForOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated { ... }
isn't a bad idea. This can be the handler for orientation change transformations for the whole view controller.
The places you need to possibly check/handle orientation issues are
-(void) viewWillAppear:(BOOL)animated
-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration
and in both of these, call updateLayoutForOrientation:animated: to do the work for you.

Resources