Creating a UIView Programatically in viewDidLayoutSubviews - ios

I want to programatically create UIViews which depend on the size of the bounds of self.view. I've put the code to create the UIViews in viewDidLayoutSubviews.
The problem is that viewDidLayoutSubviews is called multiple times when my viewController appears on screen, thus creating multiple instances of the UIView.
I'm thinking that this could be solved by using some sort of flag.
Is there a better way to do this? Should the code be put somewhere else in the view controller lifecycle?

You should not put creating UIView code in viewDidLayoutSubviews, you should create it in viewDidLoad instead. You can put view frame update code in viewDidLayout. Or you can use autolayout so you don't need any view update code manually. I prefer autolayout.

In the method of viewDidLayoutSubviews, you can get the updated the frame size for UIControls, after that you can programatically create UIViews in the viewDidAppear.
While I do not think you should create UIViews in viewDidLoad method, in autolayout you can not get correct size of views until viewDidLayoutSubviews has been called.

Related

What's exactly viewDidLayoutSubviews?

I was reading the description of viewDidLayoutSubviews of UIViewController:
Called to notify the view controller that its view has just laid out its subviews [...] However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout [...].
For me, it means: "Called when the layout of subviews is complete, but actually this is not true". So what's really behind viewDidLayoutSubviews?
When bounds change for a ViewControllers View, this method is called after the positions and sizes of the subviews have changed.
So this is our chance to make changes to view after it has laid out its subviews, but before it is visible on screen.
Any changes that depending on bounds has to be done, we can do here and not in ViewDidLoad or ViewWillAppear.
While ViewDidLoad & ViewWillAppear, the frame and bounds of a view are
not finalised. So when AutoLayout has done it's job of fixing mainView and
it's subviews, this method is called.
When using autolayout, framework does not call layoutSubviews every time. This is called in these cases.
Rotating a device: only calls layoutSubview on the parent view (the responding viewControllers primary view)
Its own bounds (not frame) changed. (The bounds are considered changed only if the new value is different, including a different origin.)
A subview is added to the view or removed from the view.
Your application forces layout to occur by calling the setNeedsLayout or layoutIfNeeded method of a view.
Scrolling a UIScrollView causes layoutSubviews to be called on the scrollView, and its superview.
Note:
The call for viewDidLayoutSubviews also depends on various factors like autoresize mask, using Auto-Layout or not, and whether view is in view hierarchy or not.
For any other clarification, check When is layoutSubviews called?
viewDidLayoutSubviews will be called when
When the bounds change for a view controller's view, the view
adjusts the positions of its subviews and then the system calls this
method.
For example you have set constraints of your view then you want to update the frame for your subview in viewDidLoad(), which will not make any impact as in viewDidLoad() your constraints are not properly set, they will get properly set when viewDidLayoutSubviews get called, now you want to update the frames of your subview, then you can do that in this method as this method get called only after all the constraints of your view are properly set.

Frames are not being updated from storyboard frames

So I want to lay out a UITableView and I want the cells to calculate their own height. I can do this with systemSizeFittingSize, but this doesn't always return the correct height, as the rows still have their frame from the storyboard (or xib).
I want to update the frames so that they are what they will be on the device.
How can I do this? I have tried setNeedsLayout and layoutIfNeeded, but I would have to do this on the highest view in the hierarchy to achieve the desired effect, as each view seems to use the frame of their superview.
I am setting the tableViewDataSource and calling reloadData in viewDidLoad in my viewcontroller.
Is there a way to set the correct frames before viewDidLoad gets called?
Edit: I want to give the height of a subview to a layout object in my viewDidLoad, so that it can work its magic providing a nice layout. Now this object only does the layout calculations. I notice that the height I request (through view.frame.size.height) in viewDidLoad is the same as in my storyboard, again telling me that in viewDidLoad the frames haven't been updated yet... Anyone with a workaround?
I think tableView:heightForRowAtIndexPath: may be something that you need:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
But setting the correct frames before viewDidLoad is possible only if you know the size before and then you can just calculate it in init.

When are the ViewWillLayoutSubviews and ViewDidLayoutSubviews methods called?

When do the ViewWillLayoutSubviews and ViewDidLayoutSubviews methods of UIViewController get called?
What is the status of the view's frame before willLayoutSubviews?
From the documentation:
When a view's bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.
viewWillLayoutSubviews gets called anytime your view controller's view has its bounds changed. This happens when the view is loaded, when a rotation event occurs, or when a child view controller has its size changed by its parent. (There are probably some other situations, too). If there is anything you need to update before that view lays itself out (and before your constraints are re-applied) you should do it here. you should generally not update constraints here, because updating constraints can cause another layout pass.
viewDidLayoutSubviews is called once all of your subviews have been laid out. If you need to fine-tune that layout by manually adjusting frames, for instance, this would be the place to do it.
View controller lifecycle can be a bit confusing, but it's worth trying to really understand if you're going to be doing much iOS development. This article is a really good overview.

When using Storyboards, why does viewWillAppear not draw my subviews and viewDidLayoutSubviews does

When using Storyboards, why does viewWillAppear not draw my subviews and viewDidLayoutSubviews does and more importantly to access the frame.size value from subviews of subviews I have to call [self.scroller layoutIfNeeded] inside of viewDidLayoutSubviews? I'm interested in understanding the page life cycle of a view controller and what changed in going from xibs to storyboards.
Storyboards are actually implemented as collections of xib files, with additional information about transitions (segues) between them. So the view controller life cycle should not be radically different if we're just talking about a single view controller.
It's very difficult to answer your specific question without understanding how your view controller and its view hierarchy are set up. It sounds like you have a view inside of a scroll view and you want to know when you can access its frame property.
UIKit follows these steps (roughly):
It loads all the views described in the storyboard/xib file and connects all the actions and outlets as needed. viewDidLoad is called after this step.
It calls viewWillAppear: to indicate that it is about to display the view.
It adds the view to the window, sizing it to fit. The sizing propagates down the view hierarchy, so each view lays out its subviews (if it is configured to autoresize subviews). These changes occur inside an animation block, so once everything is set up the user sees the new view animated into place.
Once animations are complete, viewDidAppear: is called.
It's possible you are seeing something strange if a view has autoresizesSubviews set to NO; that may be why you have to call layoutIfNeeded on self.scroller. Note that the documentation for layoutIfNeeded says:
When this message is received, the layer’s super layers are traversed until a ancestor layer is found that does not require layout. Then layout is performed on the entire layer-tree beneath that ancestor.
So it could potentially be triggering the layout of other unrelated views.

iPhone - layoutSubviews being called when updating subview

I have a custom view with a grid-like layout. When I touch each subview I want to animate them. The superview overrides layoutSubviews to create the grid of subviews.
The problem is whenever I change the frame of a subview for animation, the superview's layoutSubviews method is called overwriting the frame and no animation happens.
Why does layoutSubviews get called in this scenario and how can I prevent it?
There are other questions similar to this, but none of them have been properly answered.
Thanks!
I ended up adding a flag property on my superview and then set it to YES from my subview to prevent it from laying out subviews. Seems very hacky but it works.

Resources