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.
Related
I have a UICollectionView at the end of a UIScrollView added in the storyboard. (The scroll view's content height is fixed via autolayout), also I have returned the collection view's cell size through sizeForItemAtindexPath, but the cell size is different from what is provided and each cell have different sizes. The other behaviours like scrolling is working fine. How to set the cell size properly when the UICollectionView is inside UIScrollView?
Changing the UICollectionView's estimated cell size to None in the Identity inspector worked for me.
Check the image
I have a content view (this content view I will use as a subclass of UICollectionViewCell content later) which has an UIStackView inside. Inside that stack view, I add some labels, view. And I calculate some conditions to make sure that when data of a label is empty, that label will be hidden and the stack view will auto change the height and vice versa, if they have data I'll show them and stack view update the height again.
Now I wanna know how can I make the content view height always follow the UIStackView height whenever it's changing.
If you already made your UIStackView to have a dynamic height following its children, then all you need is to add constraints between UIStackView and your content view and it will resize automatically as well.
Now, if you add that content view inside a UICollectionViewCell, then you must make sure to call UICollectionView.reloadData() whenever you change the content of your UIStackView. That way the collection view will recalculate the size and render the cell again. You also have the option to reload a single cell in the collection view if you have a way to determine its indexPath.
Note: Take care of CollectionViewFlowLayout and what size it dictates to the cell. It's recommended that you tell the flow layout the estimated height of your cell via layout.estimatedHeight.
I need to be able to resize an UIImageView according to the size of the image which is in aspect fit.
and also i need to be able to resize the cell that the image is contained in.
A example would be the instagram app, they have different size images yet the cells are sized appropriately?
Use auto layout when creating your table view cells
Set the table view rowHeight to equalUITableViewAutomaticDimension.
Set theestimatedRowHeight or implement the height estimation delegate method.
You need to adopt the dynamic cell height. Now that cell will automatically determine its height from its content view subviews, make sure you have set your auto layout constraints carefully. Set content mode of UIImageView to aspect fit.
Below are some links to follow:
1. Rey wenderlich
Site Point
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.
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.