I have a table view with cells in my IOS app. The thing is that when the cell loads I need to layout some stuff on it that is relative to the size of other elements. The problem is that I can't get the size because there is no viewWillAppear or viewDidLoad methods, which have actual bounds(with autolayout and constraints applied). What is the best way to work with cell's geometry and sizes in IOS, can somebody tell me ?
Auto Layout and self-sizing cells (in iOS 8).
The layout system will determine the size of the "stuff" in the cells, and proper constraints will then determine the cell's height.
Related
I'm experimenting with autolayout and am running into trouble with UITableViewCell since they're created at runtime. My cells are loaded from a xib from the main ViewController. This xib has View mode set to Aspect Fill.
I've read about different ways to do this online and have yet to get any of them working. What's considered the best way to handle this?
It looks like your constraints aren't set properly, as the cell is shorter than the image's height.
Using AutoLayout and self-sizing cells is the easiest way to handle what you want to do. Once your constraints are setup properly for your custom cell, tableView:cellForRowAtIndexPath: can call dequeueReusableCellWithIdentifier:forIndexPath: and all the subview layout will be handled for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He also provides workarounds for the minor issue with the initial cell width being based on the storyboard cell, instead of the tableView width. I worked around it by setting the cell's initial width to the tableView's width, as Rasputin had suggested.
I have a collectionView inside a UITableViewCell. The collectionView's content is dynamic, hence, its height is dynamic too and the tableView cell's height depends on the collectionView's height. The collectionView cells have different sizes. The collectionView's height is always set to fit its contentSize height.
My problem is that the tableView's function heigthForRowAtIndexPath, is called before the collectionView is created (in cellForRowAtIndexPath) so, to return the height that fits the collectionView, I need to calculate it manually (which doesn't seem like a good idea for a collectionView with cells of different sizes).
I tried to use autolayout in order to use UITableViewAutomaticDimension but it didn't work (maybe I did it in a wrong way).
What is the best aproach to make a UITableViewCell consider the height of its subview in heightForRowAtIndexPath? Can I know a collectionView's estimated size without creating it?
Use self sizing, which is available in iOS 8. There are plenty of good tutorials online, like this one: http://www.appcoda.com/self-sizing-cells/.
The idea is that you can use auto layout and a few lines of code in viewDidLoad to render a table view cell that dynamically fits the content in it.
Some more tutorials:
http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html
https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8
I am looking to make an image gallery. I want my images full screen but am not sure how this works with Auto Layout. I know there is a method to return the size of a cell but didn't know if I should just be getting the screen size and returning it or if there was an approach with using Auto Layout.
The cell size in a collection view (or table view for that matter) isn't set with auto layout. You should set the size of the cell will the UICollectionViewFlowLayout method, itemSize. You can set the size of the collection view itself in IB (or code) with constraints, so it will adjust for different screen sizes, then set your cell size to be the same as the collection view's size.
I'm new to iOS development and am stucked with the problem to finish my layout with dynamic content..
Generally layout I'm trying to implement is quite popular, as an example:
and here is screenshot of my storyboard:
and table cell hierarchy:
The main question what are the main rules of building dynamic height table cell with dynamic height uiview inside it? The content could be long, so do I need to add constraints to bottom of the view?
Is it possible in Storyboard?
Thanks!
I don't think it is possible with storyboard (I might be wrong...): UITableView can have cells with a fixed size - using UITableView rowHeight property - or its delegate can provide a different size for any indexPath.
Constraints provided in Storyboard (or programmatically) only help layout the cell's subviews ONCE the cell's frame has been set.
So I think you should look to tableView:heightForRowAtIndexPath:, and a way to compute programmatically your cells' height dynamically.
I have built a very simple sample of an app (Source code on github) using a UICollectionView.
Everything is working fine, as long as the app is in portrait mode. When it is changed to landscape mode however, the content cell is not resized appropriately, and thus, nothing is displayed.
I thought that all the necessary AutoLayout constraints are in place. I am aware that I can implement collectionView:layout:sizeForItemAtIndexPath:, but my goal is to use AutoLayout as much as possible (simply to understand AutoLayout better).
What am I missing here?
You can use autolayout to set the position and size of the collection view. And you can use autolayout to set the position and size of the subviews inside each cell. But you cannot use autolayout to control the position and size of the cells. You must use the collection view's layout object to set the position and size of each collection view cell. If you want the cells to change size when the interface orientation changes, you must update your layout object to report the new size and invalidate the layout.