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."
Related
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];
});
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.
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).
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.
I've got a universal ipad/iphone app that allows the user to watch a video, which they can then expand into full screen mode.
I have implemented (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration, and in that method I perform various setFrame calls on my view elements depending on whether they are in landscape or portrait orientation.
That all seems to work fine in normal use, i.e. rotating back and forth works fine.
But if the user starts in portrait mode, starts a video, goes to full screen mode, turns into landscape orientation, and then the video stops -- the elements are often not resized properly. They appear to be sized still as if they are portrait mode.
If I then turn to portrait mode, and then turn back to landscape, the view resets correctly.
The strange part is, I have implemented (void)exitedFullscreen:(NSNotification*)notification and in there I print out the orientation, and it's seen correctly. I also call my code to reset the view elements based on the current orientation, and I am still having this problem.
Another related issue is sometimes when dealing with rotation, my views will end up too far up the screen, actually going under the status bar at the top of the device.
Edit Here's the latest example. I rotate to landscape mode during full screen video playback, and then when I left full screen video, you can see the issue with the navigation bar at the top of the view.
One possible way to solve this is by presenting your view controller modally instead of using the navigation view controller.
Refer to Kenny's answer at Problem pushViewController from Landscape to Portrait
Your ViewController might not be rotating because another controller is the first responder. What you can do to avoid this is register the view controller to the device rotation changes and implement the rotation in the selector you call when you receive such a notification.
In appDelegate:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
In your view controller
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:)name:UIDeviceOrientationDidChangeNotification object:nil];
In did rotate you can check the orientation with
[[UIDevice currentDevice] orientation]
The navigation bar at the top of the view. I solved it, using this code ->
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
Using this after your rotation.
Mason, did you logged and checked whether your method willAnimateRotationToInterfaceOrientation:duration: gets called after each state transition?
To me this latest screenshot does not look like an orientation change issue.
The navigation bar is basically off by the status bar's height.
Possibly your position calculation fails because you are using the view's frame
while the fullscreen video (w/o status bar) is playing and this fails as soon as
the statusbar is back?
Your orientation may not get updated properly if there is another controller acting as a first responder. The best way to overcome this is to call the functions you use to orientate the screen at the method viewWillAppear: using the current orientation of the view controller: [self interfaceOrientation]
If you use a subclassed subview you may need to reimplement the methot layoutSubviews and call setNeedsLayout. Another thing that may be causing this is resigning the viewcontroller where you have the video as first responder (you mays search if somewhere you use the methon resignfirstresponder and try how it works without it). If this does not work, I don't know, this things may be very tricky and dependent on how you have implemented it. But for the things you say you do you should not need much code, since automatic rotation and resizing of views is handled now by the sizes inspector of the views editor.
I think that this should do.