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).
Related
iOS 10, the gift that keeps on breaking, seems to have changed another behavior.
Assume two UIViewControllers are pushed onto a UINavigationController.
On iOS 8/9, calling navigationController?.popViewController(animated: true) to pop the top UIViewController (say VC2) caused viewDidLayoutSubviews on the bottom view controller (say VC1) to get called.
We relied on this to refresh VC1. Sometimes VC2 adds subviews to VC1 (via the data model), and this needs to get reflected when popping back to VC1.
Accurate frame information is required. We can't use viewWillAppear because frame data is wrong on iOS 9. The problem with viewDidAppear is that there is a momentary glitch between seeing the view initially and the adjustment.
Now VC1's viewDidLayoutSubviews does not get invoked when popping VC2.
1) Is this a bug for viewDidLayoutSubviews to not get invoked?
2) What's the right way to refresh view controllers when popping with UINavigationController?
Relying on viewDidLayoutSubviews was never the proper solution. UIViewController provides viewWillAppear: or viewDidAppear: for such a use. When VC2 is popped from the navigation controller, those two methods will be called on VC1 to let you know that is will be or now is visible again.
viewDidLayoutSubviews should only be used to adjust view frames and layout.
viewWill|DidAppear: should be used to handle the view controller becoming visible originally or again. In your case you should use this to update data and add/update views as needed. Those new views should be setup based on the view controller's current frame. They will be adjusted in your implementation of viewDidLayoutSubviews as needed.
I will complement rmaddy's answer. You need to decouple performing layout and updating your data. If your flow is such that data should updated as the view is about to appear, you should update your controller's view-backing data in viewWillAppear:, reload your views and then mark the view as needing layout using setNeedsLayout. This will cause the system to perform a layout on the controller's view, and will trigger the layout. This way, you can ensure the layout is performed once the view is ready, and not before (as often the case is in viewWillAppear:.
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 want the keyboard to slide up as the view controller slides up. But for one of my view controllers that I present modally, the keyboard appears instantly when the view controller is presented, so the keyboard appears then the view controller slides up from under it, causing an ugly effect.
Oddly enough, this instantaneous behaviour happens when it's in viewDidLoad, but having it there works fine for another view controller. (But in the instantaneous one it appears for a UITextField, while the proper one is for a UITextView.)
Here's what the code looks like:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.URLTextField becomeFirstResponder];
}
How do I make it present alongside the view controller? I don't have to do an ugly dispatch_after do I?
If it's loading too quickly with some methods (ViewDidLoad/ViewWillAppear) and too slowly with others you could try doing something in the middle.
I wouldn't suggest it as I'm sure theres a way to have it do what you like but I imagine that in viewDidLoad you can set the view up to respond to to keyboardWillShow and then become first responder and in the notification delay for a few milliseconds
I created regular buttons in .xib file and I added a gradient effect to them and shadows in the code in this section:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
... my customized buttons code here
}
When I modally switch to another view controller and then go back to the original one the xib file gets redrawn but all the gradient effects and shadows disappear. Any ideas?
I'm not sure entirely what's going on, but note that after you dismiss the modal view controller, viewDidAppear: gets called again. If you only want to make these buttons once, you could move your custom button code to viewDidLoad.
I haven't experienced this problem before to know what's going on, so it would be helpful to see the code itself. But I suspect that moving the code to viewDidLoad would solve the problem.
When modal View is dismissed, ViewDidLoad is not called, But ViewWillAppear and ViewDidAppear is called, But you say that the code is written in ViewDidAppear, and still it doesnt work. I suggest you to write that code in ViewWillAppear and check.
I have a modal view which is launched from the current view controller, as
[self presentModalViewCOntroller:modalViewController animated:TRUE];
The modal view controller dismisses itself when someone hits a button.
[self dismissModalViewControllerAnimated:TRUE];
A couple of screens later, I attempt to swap the root view within the window. I do this all the time with no trouble. But in a certain case, when switching the one view within the window, the picker delegate method is being called on the modal view controller even thought it was dismissed a while ago.
This is very strange because the modal view controller is usually deallocated when dismissModalViewController is called.
Why is a view from the modal view controller being invoked?
It appears that someone, probably the window still has a reference. Are you supposed to do something else in addition to dismissModalViewController?
Thanks
DismissModalViewController should be enough. It does seem like you have a problem with some reference hanging around that you don't intend. Without seeing more code, I can't point to anything specific.